Home > Enterprise >  Adding one read only attribute to Core Data Model in Swift. Which doesn't exist in XCDataModel
Adding one read only attribute to Core Data Model in Swift. Which doesn't exist in XCDataModel

Time:01-04

Adding attribute to entity which don't exist in XCDataModel was easily accomplised VIA @synthesize and @dynamic in objective c. Please guide how to achieve this in swift.

@NSManaged public var salesPrice: NSNumber?
public var totalVal: NSNumber {
   get {
       return salesPrice ?? NSNumber(value: 0)
   }
}

Here sales price is managed but totalVal is not managed nor exist in xcDataModel. I am getting crash 'The entity is not key value coding-compliant for the key 'totalVal' on accessing totalVal.

if let coreObj = indItem.parentObj.value(forKey: keyPath) { }

Crash happening here.

  • 'indItem.parentObj' this is right object (I double checked)
  • 'keyPath' is 'totalVal' (I copied it rightly)

CodePudding user response:

To support key-value coding for a type declared in Swift, you have to mark it with the @objc keyword:

@objc public var totalVal: NSNumber {
  •  Tags:  
  • Related