谓词在 CoreData 中不起作用 - Swift 4
Predicate doesn't work at CoreData - Swift 4
我正在尝试加载索引为 1 的名称,但它不起作用(CollectionView 为空)。
核心数据:
加载数据:
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)")
}
属性类型和谓词类型不匹配。
Transformable
作为 index 的类型无论如何都是荒谬的。将属性类型设置为 Int32
,或者如果只有 0
和 1
索引甚至设置为 Bool
.
我正在尝试加载索引为 1 的名称,但它不起作用(CollectionView 为空)。
核心数据:
加载数据:
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)")
}
属性类型和谓词类型不匹配。
Transformable
作为 index 的类型无论如何都是荒谬的。将属性类型设置为 Int32
,或者如果只有 0
和 1
索引甚至设置为 Bool
.