我有一个类(TestClass),它有两个属性ID:String和Code:Int。
class TestClass : NSObject , NSCoding {
@objc let ID : Int32
@objc let Code : String
init(id : Int32, code : String) {
self.ID = id
self.Code = code
}
public func encode(with aCoder: NSCoder) {
aCoder.encode(ID, forKey: "ID")
aCoder.encode(Code, forKey: "Code")
}
required public convenience init?(coder aDecoder: NSCoder) {
guard let id = aDecoder.decodeObject(forKey: "ID") as? Int32, let code = aDecoder.decodeObject(forKey: "Code") as? String else {
return nil
}
self.init(id: id, code: code)
}}
我将此类用于我的可转换类型。
@NSManaged public var trnsf: TestClass?
每件事看起来都很好,甚至我可以根据ID对我的获取请求进行排序
let SortDes = NSSortDescriptor(key: #keyPath(SomeEntity.trnsf.ID), ascending: true)//delete
问题:
我无权访问名称字符串的 NSPredicate
let Prdct = NSPredicate(format: "%K == %@" ,#keyPath(SomeEntity.trnsf.Code) , "SomethingString")
请注意:
- 我知道 CoreData 将我的课程保存为 A Data,只有我可以使用“%K == %@”
- 我知道CoreData 中的关系,但我不想在这种情况下使用它
它的工作方式如下,但我想比较属性而不是所有类,因为我不知道 id
let Prdct = NSPredicate(format: "%K == %@" , #keyPath(SomeEntity.trnsf.ID) , TestClass(id: 1, code: text))