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

163 Views Asked by At

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)
1

There are 1 best solutions below

1
jrturton On BEST ANSWER

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 {