Christian Gossain
Christian Gossain

Reputation: 5972

How to store a boolean value to an NSArray?

So I'm storing user settings in a plist file and to do that I'm adding data to an NSArray. This approach is working for me.

My problem is that now I'm adding a UISwitch to the settings and I was wondering how to store their ON/OFF state to the array so that I can access that state at a later time?

I'm adding data to the array like this:

[array addObject: mySwitch.on];

Then I'm trying to set the state like this:

[mySwitch setOn:[array objectAtIndex:0]];

Upvotes: 7

Views: 7774

Answers (1)

BoltClock
BoltClock

Reputation: 723648

Since NSArray only takes in (id)s (i.e. Objective-C pointers to objects), you can only store objects.

The common way to store a BOOL value in an object is with the NSNumber class:

[array addObject:[NSNumber numberWithBool:mySwitch.on]];

To access it, grab that NSNumber object and send it a boolValue message:

[mySwitch setOn:[[array objectAtIndex:0] boolValue]];

Upvotes: 34

Related Questions