skålfyfan
skålfyfan

Reputation: 5281

How to correctly set a UISwitch ON state using a NSManagedObject boolean attribute type?

Lets say I have the following setup:

....
UISwitch gpsSwitch;

// the below object has a gpsEnabled property of type boolean - default NO
NSManagedObject *typeObject;
....

How exactly do I go about properly calling:

self.gpsSwitch.on = [self.typeObject valueForKey:@"gpsEnabled"];

This always returns warning: passing argument 1 of 'setOn:' makes integer from pointer without a cast. and does not work.

I've tried:

(BOOL)[self.typeObject valueForKey:@"gpsEnabled"];
(NSInteger)[self.typeObject valueForKey:@"gpsEnabled"];

and none of these casts seem to work? What's fix? :)

Upvotes: 0

Views: 1667

Answers (1)

amattn
amattn

Reputation: 10065

[self.typeObject valueForKey:@"gpsEnabled"] returns and NSNumber object.

[[self.typeObject valueForKey:@"gpsEnabled"] boolValue] returns a BOOL.

self.gpsSwitch.on needs a BOOL.

Even though you setup boolean or int or whatever in your core data model, they are always retreived/set as NSNumbers. Somewhat confusing. I like to make accessors in my NSManagedObject subclasses that do the conversion for me.

Upvotes: 1

Related Questions