我正在尝试从notification.userInfo插入和删除的值中获取位置对象的数组,并相应地更新地图注释。它不允许我将其强制转换为类型[Location]
。
我收到以下错误
无法将类型'__NSCFSet'(0x109b56408)的值强制转换为'NSArray'
var managedObjectContext: NSManagedObjectContext! {
didSet {
NotificationCenter.default.addObserver(forName: Notification.Name.NSManagedObjectContextObjectsDidChange, object: managedObjectContext, queue: OperationQueue.main) { (notification) in
if self.isViewLoaded {
if let dictionary = notification.userInfo {
print(dictionary["inserted"])
if let inserted = dictionary["inserted"]{
var location = [Location]()
location = inserted as! [Location]
}
if let deleted = dictionary["deleted"] {
}
}
}
}
}
}
答案 0 :(得分:0)
数据不是数组,而是集合。并使用适当的键来访问数据。
var managedObjectContext: NSManagedObjectContext! {
didSet {
NotificationCenter.default.addObserver(forName: Notification.Name.NSManagedObjectContextObjectsDidChange, object: managedObjectContext, queue: OperationQueue.main) { (notification) in
if self.isViewLoaded {
if let dictionary = notification.userInfo {
print(dictionary[NSInsertedObjectsKey])
if let inserted = dictionary[NSInsertedObjectsKey] as? Set<Location> {
var location = Array(inserted)
}
if let deleted = dictionary[NSDeletedObjectsKey] {
}
}
}
}
}
}