0

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.

gerry3
  • 21,420
  • 9
  • 66
  • 74
Jab
  • 26,853
  • 21
  • 75
  • 114

3 Answers3

3

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

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
  • 21,420
  • 9
  • 66
  • 74
0

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

Paul Peelen
  • 10,073
  • 15
  • 85
  • 168
  • Yes, but how do I save an integer using NSData or at least into a file. That is what I am asking – Jab Jan 06 '10 at 17:26