问题
I have A class (TestClass) and it has two Properties ID : String and 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)
}}
I use this class for my Transformable Type.
@NSManaged public var trnsf: TestClass?
Every thing Looks so nice, even I can sort my fetch request based by ID
let SortDes = NSSortDescriptor(key: #keyPath(SomeEntity.trnsf.ID), ascending: true)//delete
The Problem:
I don't have access to NSPredicate for Name String
let Prdct = NSPredicate(format: "%K == %@" ,#keyPath(SomeEntity.trnsf.Code) , "SomethingString")
please Attention that:
- I know CoreData save my class as A Data and only I can use "%K == %@"
- I know about relations in CoreData but I don't want to use it in this case
it's working like below but I want to compare Properties and not all class, because I don't know the id
let Prdct = NSPredicate(format: "%K == %@" , #keyPath(SomeEntity.trnsf.ID) , TestClass(id: 1, code: text))
来源:https://stackoverflow.com/questions/47223862/using-nspredicate-with-transformable-attribute-in-core-data