views:

124

answers:

3

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.

A: 

You could save it as an integer (1 / 0 = true / false)?

Paul Peelen
Yes, but how do I save an integer using NSData or at least into a file. That is what I am asking
Jaba
+1  A: 

You can convert a boolean to an NSNumber using the convenience constructor (class method) numberWithBool:.

NSNumber can be persisted by adding it to an NSDictionary and using writeToFile:atomically: to save the dictionary as a property list (plist).

NSNumber objects are also used to represent and persist booleans (as well as integers and floats, but not currency) in Core Data.

gerry3
+3  A: 

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"];
DyingCactus