开发者

How to store a boolean value to an NSArray?

开发者 https://www.devze.com 2023-01-28 06:48 出处:网络
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.

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开发者_StackOverflow中文版:0]];


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

精彩评论

暂无评论...
验证码 换一张
取 消