谓词在CoreData上不起作用-Swift 4

时间:2019-02-25 08:06:29

标签: ios swift core-data nspredicate

我正在尝试使用索引1加载Name,但是它不起作用(CollectionView为空)。

CoreData

enter image description here

LoadData:

guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }

let managedContext = appDelegate.persistentContainer.viewContext
let fetchRequestTag = NSFetchRequest<NSManagedObject>(entityName: "Tag")
let PreValue = 1
let predicate = NSPredicate(format: "index == %i", PreValue)
fetchRequestTag.predicate = predicate

do {
   tagItems = try managedContext.fetch(fetchRequestTag)
} catch let error as NSError {
   print("Could not fetch. \(error), \(error.userInfo)")
}

TagCollectionView.reloadData()
TagCollectionView.selectItem(at: NSIndexPath(item: 0, section: 0) as IndexPath, animated: false, scrollPosition: [])

for data in tagItems {
   print("\(data.value(forKey: "name") as! String as Any), \(NSKeyedUnarchiver.unarchiveObject(with: data.value(forKeyPath: "index") as! Data) as! Int)")
}

1 个答案:

答案 0 :(得分:3)

属性类型和谓词类型不匹配。

Transformable作为 index 的类型还是毫无意义的。将属性类型设置为Int32,或者将01的索引设置为Bool