[swift-users] CoreData weirdness in development build

Brent Royal-Gordon brent at architechies.com
Wed Jun 8 04:22:51 CDT 2016

> 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 {
		@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

More information about the swift-users mailing list