@distinctUnionOfObjects 可以在 Swift 中使用吗?
Posted
技术标签:
【中文标题】@distinctUnionOfObjects 可以在 Swift 中使用吗?【英文标题】:is @distinctUnionOfObjects usable in Swift? 【发布时间】:2016-10-12 08:39:16 【问题描述】:我正在尝试从this 很好的例子开始快速制作日历。到目前为止,我设法展示了自定义 UICollectionViewLayout
的网格和所有补充视图。问题是当我尝试在日历中添加事件时。日期行和日历都在同一个NSMutableDictionary
中,它有一个使用此格式2016-10-12
表示日期(字符串)的键。目标c代码是
-(void)groupEventsBySection
//toDeviceTimezoneDateString simply take a Date and make a string
_eventsBySection = [mEvents groupBy:@"StartDate.toDeviceTimezoneDateString"].mutableCopy;
// after groupBy is performed _eventsBySection contains only those keys found in mEvents with inside an array of objects
// "2016-10-12" = (
// "<MSEvent: 0x60000004a680>",
// "<MSEvent: 0x60000004a380>");
// "2016-10-13" = (
// "<MSEvent: 0x600000049b10>");
// "2016-10-17" = (
// "<MSEvent: 0x600000049fc0>");
//
NSDate* date = [NSDate today:@"device"]; // today's date
if(self.daysToShow == 1 && _eventsBySection.count == 1)
date = [NSDate parse:_eventsBySection.allKeys.firstObject];
//here it adds the remaining "daysToShow" if the key doesn't already exist in the MutableDictionary.
for(int i = 0; i< self.daysToShow; i++)
if(![_eventsBySection.allKeys containsObject:date.toDeviceTimezoneDateString])
[_eventsBySection setObject:@[] forKey:date.toDeviceTimezoneDateString];
date = [date addDay]; // this just add one day to date
到目前为止,这很清楚。问题是当我尝试更改目标 c 中的 groupBy
函数时:
- (NSDictionary*)groupBy:(NSString*)keypath
return [self groupBy:keypath block:^NSString *(id object, NSString *key)
return key;
];
- (NSDictionary*)groupBy:(NSString*)keypath block:(NSString*(^)(id object, NSString* key))block
NSMutableDictionary *result = [NSMutableDictionary new];
NSString* finalKeypath = [NSString stringWithFormat:@"%@.@distinctUnionOfObjects.self",keypath];
NSArray *distinct = [self valueForKeyPath:finalKeypath];
[distinct each:^(NSString* value)
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K = %@", keypath,value];
NSArray *objects = [self filteredArrayUsingPredicate:predicate];
[result setObject:objects forKey:block(objects[0],value)];
];
return result;
- (NSDictionary*)expand:(NSString*)keypath
return [self expand:keypath unique:NO];
到目前为止,这是我在 swift 中所做的:
我有这个类,在原始代码中是MSEvent
class Event : NSObject
var title:String?
var location:String?
var startTime:Date?
var endTime:Date?
var duration:Int?
var subtitle:String?
init(startTime: Date, duration: Int, title: String, subtitle: String)
super.init()
self.startTime = startTime
self.duration = duration
self.title = title
self.subtitle = subtitle
我创建了一些假事件:
override func viewDidLoad()
let today = Date()
let event1 = Event(startTime: today, duration: 60, title: "prova", subtitle: "HEllo")
let event2 = Event(startTime: today, duration: 60, title: "prova", subtitle: "HEllo")
let event3 = Event(startTime: today.dateByAddingDays(days: 1), duration: 60, title: "prova", subtitle: "HEllo")
var array:[Event] = []
array.append(event1)
array.append(event2)
array.append(event3)
calendarView.setEvents(events: array)
func setEvents(events:[Event])
mEvents = events as NSArray
print(mEvents)
//this right now returns the 3 event objects I added:
// (
// "<myapp.Event: 0x60800032ba40>",
// "<myapp.Event: 0x60800032bae0>",
// "<myapp.Event: 0x60800032bcc0>"
// )
self.forceReload(reloadEvent: true) //force reload eventually call the groupEventsBy days
func groupEventsByDays()
// of course everything works without the next line of code
eventsBySection = mEvents.groupBy(keypath: "startTime").mutableCopy() as! NSMutableDictionary
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
var date = dateFormatter.date(from: "2016-10-01")
// right now daysToShow is equal to 30
for _ in 0..<daysToShow
eventsBySection.setObject([], forKey: self.setFormatDate(date: date!) as NSCopying)
date = self.addDay(date: date!)
这就是问题所在。这是我的扩展。 扩展 NSArray
func groupBy(keypath:NSString)->NSDictionary
return self.groupBy(keypath: keypath, block: (object, key) -> NSString in
return key
)
func groupBy(keypath:NSString,block:@escaping ((_ object: Any, _ key:NSString )-> NSString))-> NSDictionary
let result:NSMutableDictionary = NSMutableDictionary()
let finalKeypath = String.localizedStringWithFormat("%@.distinctUnionOfObjects.self", keypath)
let distinct:NSArray = self.value(forKey: finalKeypath) as! NSArray
(distinct as AnyObject).each(operation: (value) in
let predicate = NSPredicate(format: "%K = %@", keypath,value as! CVarArg)
let objects = self.filtered(using: predicate)
result.setObject(objects, forKey: (block(objects[0], value as! NSString)))
)
return result;
func each(operation:@escaping ((_ object: AnyObject)-> Void))
self.enumerateObjects( (object, idx, stop) in
operation(object as AnyObject)
)
它因错误this class is not key value coding-compliant for the key startTitle.distinctUnionOfObjects.self.'
而崩溃
我试过像这样使用map
let startDate = array.map ( $0.startTime )
我设法获得了所有日期(虽然没有不同),但我不知道如何使用 swift 实现上述代码中显示的相同结果。
【问题讨论】:
它应该可以工作,但是self
你在哪里查询 NSObject 的子类,并且 startTitle 是一个对 Objective-C 运行时可见的属性? (即它是否属于与 Objective-C 兼容的类型 - 我根据其名称推测 String
?)
啊,没关系,它在一个 NSArray 扩展中,所以self
确实是。但是,关于startTitle
的问题仍然存在。
【参考方案1】:
我终于设法用@distinctUnionOfObjects.sel
完成了上述目标c 代码所做的事情。不过,我不确定这是一种优雅的方式。欢迎所有 cmets 或更好的答案。
extension Collection
func find( predicate: (Self.Iterator.Element) throws -> Bool) rethrows -> Self.Iterator.Element?
return try index(where: predicate).map(self[$0])
override func viewDidLoad()
super.viewDidLoad()
let event1 = Event(startTime: today!, duration: 60, title: "prova", subtitle: "HEllo")
let event2 = Event(startTime: (today?.dateByAddingDays(days: 5))!, duration: 60, title: "prova", subtitle: "HEllo")
let event3 = Event(startTime: (today?.dateByAddingDays(days: 1))!, duration: 60, title: "prova", subtitle: "HEllo")
let event4 = Event(startTime: todayLater!, duration: 60, title: "prova", subtitle: "HEllo")
var array:[Event] = []
array.append(event1)
array.append(event2)
array.append(event3)
array.append(event4)
// I create an array with all the date/startTime
let startTime = array.map ( $0.startTime )
let dict:NSMutableDictionary = [:]
for date in startTime
//find the objects for that date
let object = array.find(predicate: $0.startTime?.toDeviceDateString() == date?.toDeviceDateString())
//check if that key/date exist in the dictionary
if var val:[Event] = dict[(date?.setFormatDate())!] as! [Event]?
//if it does I simply add the object to the array
val.append(object!)
dict.setObject(val, forKey: date?.setFormatDate() as! NSCopying)
else
print("key is not present in dict")
//otherwise I add a new array with that object inside for the date/key
dict.setObject([object], forKey: date?.setFormatDate() as! NSCopying)
calendarView.setEvents(events: dict)
func setEvents(events:NSMutableDictionary)
//slots events hold the events objects
slotEvents = events
self.forceReload(reloadEvent: true)
func groupEventsByDays()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
var date = dateFormatter.date(from: "2016-10-01")
for _ in 0..<daysToShow
if let val:[Event] = slotEvents[(date?.setFormatDate())!] as! [Event]?
eventsBySection.setObject(val, forKey: date?.setFormatDate() as! NSCopying)
else
eventsBySection.setObject([], forKey: date?.setFormatDate() as! NSCopying)
date = self.addDay(date: date!)
print("after\(eventsBySection)")
//Which print
//
//"2016-10-01" = (
// );
//"2016-10-02" = (
// );
// "2016-10-03" = (
// "Optional(<myapp.Event: 0x6000001242e0>)",
// "<myapp.Event: 0x6000001242e0>"
// );
// "2016-10-04" = (
// "Optional(<myapp.Event: 0x600000124600>)"
// );
// "2016-10-05" = (
// );
// "2016-10-06" = (
// );
// "2016-10-07" = (
// );
// "2016-10-08" = (
// "Optional(<myapp.Event: 0x600000124240>)"
// );
// "2016-10-09" = (
// );
//.... and so on up to 30 days
【讨论】:
以上是关于@distinctUnionOfObjects 可以在 Swift 中使用吗?的主要内容,如果未能解决你的问题,请参考以下文章