CoreData weirdness in development build


(Charles Lane) #1

Here’s something ‘cute’. Make a simple ios project using coredata. Name an entity whatever you like, but give it any attribute that starts with ‘is’. Such as isVisited or isDone or whatever you like of type Boolean. Try setting the entity to true or false, no matter, and save the entity. The project will fail saying : 2016-06-07 15:10:55.601 TestCoreData[5269:1527094] -[TestCoreData.HoldBoolean setDone:]: unrecognized selector sent to instance 0x7fd9fbc59380. (HoldBoolean was the entity name in the project)
The attribute in that case was set to ‘isDone’, if the attribute had been named ‘isWhatever’, it would have failed saying TestCoreData.HoldBoolean setWhatever:]
CoreData won’t accept any attribute starting with ‘is’. This is with the latest June 6 development build but it was the same in the May 31 build. This may only affect attributes of type boolean, I haven’t checked that.

Or am I doing something stupid?


(Charles Lane) #2

OK, i tested it using a string attribute and it still fails. Coredata will crash if an attribute begins with ‘is’.

···

Here’s something ‘cute’. Make a simple ios project using coredata. Name an entity whatever you like, but give it any attribute that starts with ‘is’. Such as isVisited or isDone or whatever you like of type Boolean. Try setting the entity to true or false, no matter, and save the entity. The project will fail saying : 2016-06-07 15:10:55.601 TestCoreData[5269:1527094] -[TestCoreData.HoldBoolean setDone:]: unrecognized selector sent to instance 0x7fd9fbc59380. (HoldBoolean was the entity name in the project)
The attribute in that case was set to ‘isDone’, if the attribute had been named ‘isWhatever’, it would have failed saying TestCoreData.HoldBoolean setWhatever:]
CoreData won’t accept any attribute starting with ‘is’. This is with the latest June 6 development build but it was the same in the May 31 build. This may only affect attributes of type boolean, I haven’t checked that.

Or am I doing something stupid?


(Brent Royal-Gordon) #3

Here’s something ‘cute’. Make a simple ios project using coredata. Name an entity whatever you like, but give it any attribute that starts with ‘is’. Such as isVisited or isDone or whatever you like of type Boolean. Try setting the entity to true or false, no matter, and save the entity. The project will fail saying : 2016-06-07 15:10:55.601 TestCoreData[5269:1527094] -[TestCoreData.HoldBoolean setDone:]: unrecognized selector sent to instance 0x7fd9fbc59380. (HoldBoolean was the entity name in the project)
The attribute in that case was set to ‘isDone’, if the attribute had been named ‘isWhatever’, it would have failed saying TestCoreData.HoldBoolean setWhatever:]
CoreData won’t accept any attribute starting with ‘is’. This is with the latest June 6 development build but it was the same in the May 31 build. This may only affect attributes of type boolean, I haven’t checked that.

Or am I doing something stupid?

Objective-C has a sort of informal, unevenly-used convention that the setter corresponding to `-isFoo` is `-setFoo:`, not `-setIsFoo:`. I suspect that Core Data is trying to follow this convention, but Swift doesn't generate setters using this pattern.

It would be nice if you could write something like this:

  var isFoo: Bool {
    get
    @objc(setFoo:) set
  }

But unfortunately you don't seem to be able to. Perhaps the simplest fix is to say:

  @objc(foo) var isFoo: Bool

And adjust your managed object model accordingly.

···

--
Brent Royal-Gordon
Architechies


(Charles Lane) #4

Thanks Brent, sounds reasonable. Although I don’t think this was a problem in pre-Swift 3.0 builds. I think I’ll just avoid using attributes beginning with ‘is’ in the managed object model!