我正在尝试从 notification.userInfo 中获取一组位置对象,插入和删除值并相应地更新 map 注释。它不允许我将其转换为 [Location]
类型。
出现以下错误
Could not cast value of type '__NSCFSet' (0x109b56408) to '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"] {
}
}
}
}
}
}
最佳答案
数据不是数组,是集合。并使用适当的 key 访问数据。
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] {
}
}
}
}
}
}
关于swift - 无法将类型 '__NSCFSet' (0x109b56408) 的值转换为 'NSArray' Swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53327347/