Search code examples
iosxcodeios7nsuserdefaults

How to Store and Load a BOOL array or int Array with NSUserDefaults? (iOS7)


I'm building an iOS game app and I'm basically trying to store in NSUserDefaults a BOOL array of levels (to see which level is unlocked) and an int array that stores the number of points the player earned in each level.

Thanks.


Solution

  • You can store Booleans in an array like this:

    NSArray *boolArray = @[@YES,@NO,@YES];
    

    And Integers like this:

    NSArray *intArray = @[@1,@2,@3];
    

    You can then save your array to NSUserDefaults (although I really don't recommend doing that) like this:

    [[NSUserDefaults standardUserDefaults] setObject:boolArray forKey:@"myBoolArray"];
    

    And you can later retrieve it like this:

    NSArray *retrievedArray = (NSArray *)[[NSUserDefaults standardUserDefaults] objectForKey:@"myBoolArray"];