PDA

View Full Version : Blank nsinteger




lowcountry01
Aug 8, 2010, 02:57 PM
So I have a NSInteger stored in NSUserDefaults that I change from time to time when the app is running. But if the app has never been run, the request to get the nsinteger should return nothing. The request code is this (nsuserdefaults has already been declared):

NSInteger myInteger = [myUserDefaults integerForKey:@"exampleKey"]

Then I pass an if...then statement so that if the integer is nothing, then I will make it equal 1.
I tried to say if (myInteger == nil)... but because a nsinteger doesn't use a pointer, it cannot be nil.

So I guess my question then is, what would it be? What would I test the NSInteger against if it isn't nil?



braves4life
Aug 8, 2010, 03:05 PM
Look up registerDefaults. This will let you specify what that key stores if it has not yet been set.

chown33
Aug 8, 2010, 03:24 PM
Go to the reference doc for NSUserDefaults.
http://developer.apple.com/iphone/library/documentation/cocoa/reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html

Find the section on integerForKey.

Read what it says: "If the specified key does not exist ...".

lowcountry01
Aug 8, 2010, 06:06 PM
Thanks! That's exactly what I wanted but just didn't know where to look!