Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data Scalar error only on some devices

I've been testing primarily using iPhone 6, 6 plus, and iPad. Just tried my app today on the iPhone 5 simulator, and got a Core Data error.

CoreData: error: Property 'setId:' is a scalar type on class 'AppName.EntityName' that does not match its Entity's property's scalar type. Dynamically generated accessors do not support implicit type coercion. Cannot generate a setter method for it

Now, there is no 'setId' object in my app, but of course the entity does have an 'ID' object, which is set as an int.

class Entity: NSManagedObject {
    @NSManaged var id: Int
}

In the Core Data model, the attribute type is set to Integer64. That might be my problem, as I picked that without knowing what was best. I have other attributes in the model and class, but they are all strings.

Looking for both a fix and an explanation as to why this happens only on some devices, so I can learn!

like image 643
Tim Avatar asked May 24 '15 14:05

Tim


2 Answers

If the Core Data type is Integer 64 then you should declare the property as Int64 (or let Xcode create the managed object subclass).

Int can be 32 bit or 64 bit, depending on the processor architecture.

Alternatively, define the property as NSNumber* instead of a scalar property. Of course you have to take care that on a 32-bit platform the values do not exceed the range of Int.

like image 183
Martin R Avatar answered Nov 16 '22 06:11

Martin R


Alternatively, you can write your own setter/getter method that transforms the Int into a NSNumber instance and passes this on the the primitive value. E.g.:

private(set) var publicId: Int {
  get {
    self.willAccessValueForKey("publicId")
    let value = self.primitivePublicId.longValue
    self.didAccessValueForKey("publicId")
    return value
  }
  set {
    self.willChangeValueForKey("publicId")
    self.primitivePublicId = NSNumber(long: newValue)
    self.didChangeValueForKey("publicId")
  }
}
@NSManaged private      var primitivePublicId: NSNumber
like image 28
Tobias Avatar answered Nov 16 '22 05:11

Tobias