Core Data Primary Key

前端 未结 7 703
無奈伤痛
無奈伤痛 2020-11-28 02:37

This may seem stupid, but I still couldn\'t figure out how to mark a attribute as a primary key in the xcdatamodel file. My persistent storage is sqlite file. Can anyone hel

相关标签:
7条回答
  • 2020-11-28 03:12

    A Swift Extension to retrieve the primary key

    extension NSManagedObject {
        var primaryKey : String {
            guard objectID.uriRepresentation().lastPathComponent.count > 1 else { return "" }
            return objectID.uriRepresentation().lastPathComponent.substring(from: 1)
        }
    }
    

    And for String

    extension String
    {
        func substring(from : Int) -> String {
            guard self.count > from else { return "" }
            return String(self[self.index(self.startIndex, offsetBy: from)...])
         }
    }
    
    0 讨论(0)
提交回复
热议问题