On Jan 13, 2010, at 7:29 PM, Richard Somers wrote:

> On Jan 13, 2010, at 7:14 AM, Jim Correia wrote:
> 
>> This style is typically used for non-object BOOL values. If you use it for 
>> an NSNumber attribute, you run the risk of someone familiar with the pattern 
>> assuming it is a BOOL property and writing
>> 
>>      if ([managedObject isSelected]) {
>> 
>>      }
>> 
>> which will of course be be true whenever the property is non-nil, no 
>> regardless of the actual value.
> 
> Thanks for the reply and the information. The attribute is a bool in the 
> Xcode data modeler. When you copy the Obj-C 2.0 method declarations for the 
> bool attribute to the clipboard this is what you get.
> 
>     @property (nonatomic, retain) NSNumber *selected;
> 
> So I assume that Core Data frameworks default is to use a NSNumber for a bool 
> value.

That is correct. Core Data always expresses its attribute as object values.

If you want to have a scalar BOOL property on your object, you can, but you 
must write your own accessors. (And be aware of and deal with 
-setNilValueForKey: as appropriate.)

For example:

- (BOOL)isSelected 
{
    NSNumber *selected = nil;
    
    [self willAccessValueForKey: @"selected"];
    selected = [self primitiveSelected];
    [self didAccessValueForKey: @"selected"];
    
    return (selected != nil) ? [selected boolValue] : NO;
}

- (void)setSelected:(BOOL)selected 
{
    [self willChangeValueForKey: @"selected"];
    [self setPrimitiveSelected: [NSNumber numberWithBool: selected]];
    [self didChangeValueForKey: @"selected"];
}

- Jim



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to