Using NSPredicate with transformable attribute in Core Data

你离开我真会死。 提交于 2019-12-11 14:45:43

问题


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:

  1. I know CoreData save my class as A Data and only I can use "%K == %@"
  2. I know about relations in CoreData but I don't want to use it in this case
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!