Why does the property declaration set a boolean attribute's type to NSNumber when having Xcode automatically generate managed object class files?

StackOverflow https://stackoverflow.com/questions/8552447

سؤال

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?

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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];.

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top