Search code examples
iphonepersistencebooleannsdatacocoa-design-patterns

How would I save and retrieve a boolean into a file using NSData


I want to save a boolean into a file and also I want to know how you would retrieve it again. I mainly want to use this for when the iPhone terminates and finishes launching in their respected methods.


Solution

  • An alternative to manually saving to a file is to use NSUserDefaults.

    To save:

    [[NSUserDefaults standardUserDefaults] setBool:myBool forKey:@"myBool"];
    

    To load:

    myBool = [[NSUserDefaults standardUserDefaults] boolForKey:@"myBool"];