Why does the property declaration set a boolean attribute's type to NSNumber when having Xcode automatically generate managed object class files?
-
20-03-2021 - |
سؤال
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.
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.