1

When I have Xcode automatically create the class files for the entities in the data model, any attributes I've specified as Boolean get a type of NSNumber in the property declaration:

@property (nonatomic, retain) NSNumber * isGood;

Since I'm using ARC, shouldn't it be strong rather than retain?

Perhaps this happens because Boolean can't be converted to an object. Is that correct? If so, how can I use attributes that should have boolean values?

maxedison
  • 17,243
  • 14
  • 67
  • 114

3 Answers3

2

When generating classes for a core data model, there is a checkbox "Use scalar properties for primitive types". If you select that, it will use a BOOL type for your boolean properties. Since a primitive type is now being used, it will also not contain any retain (or strong) keywords in the declaration.

Tim Dean
  • 8,253
  • 2
  • 32
  • 59
  • Is there a reason NOT to check that box? In other words, any reason why leaving it as an NSNumber is beneficial? And what about integer attributes? Should I check that scalar box for those as well? – maxedison Dec 18 '11 at 15:27
  • At one point I remember reading that using scalar properties excessively can have a performance impact due to the way core data requires getters and setters for scalar properties to be implemented. However, I don't know if that is still true, or if it was ever true for that matter. The performance has been adequate in my apps that use scalar properties, so I've never needed to do a thorough performance optimization in that area. – Tim Dean Dec 19 '11 at 00:32
  • But I would have to implement my own custom getters & setters for scalar properties, correct? – maxedison Dec 19 '11 at 16:39
  • I have not had to do that, at least not at this point. When you check the "Use scalar properties" option under the current version of Xcode it takes care of everything. I haven't done a full analysis to see what is going on there, but it works for me (so far) without implementing custom getters or setters – Tim Dean Dec 19 '11 at 18:14
  • Discussion on pros/cons of scalar can be found on the Apple dev site: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdNSAttributes.html – Sofi Software LLC Feb 03 '12 at 22:22
2

Yes, NSNumber is used as BOOL isn't an object. The same holds for CGFloat and NSInteger and the like.

To get the BOOL value from NSNumber use [myNSNumber boolValue];.

dasdom
  • 13,975
  • 2
  • 47
  • 58
0

I've read the conversion between maxedison and Tim Dean, and I've double checked it. It turns out that Tim might not be right. You have to implement the getter and setter.

According to http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdNSAttributes.html#//apple_ref/doc/uid/TP40001919-SW13

You can declare properties as scalar values, but for scalar values Core Data cannot dynamically generate accessor methods—you must provide your own implementations

And the pros and cons (in the same document):

The advantages of allowing Core Data to manage its own storage usually outweigh any advantages of interacting directly with scalar values, although if you suspect that this is not true for your application you should use performance analysis tools to check.

tzuchien.chiu
  • 700
  • 6
  • 11