views:

1034

answers:

3

Alright, so I'm working on a small iPhone app, and I am using NSUserDefaults as my data persistence. It only has to keep track of a few things, such as some names and some numbers so I figure I might as well keep it simple.

I found this page for some reference, but I don't think it can answer my question. Basically, I want to be able to check if a value (or a key) already exists in the NSUserDefaults and then do something accordingly.

Some examples: The app starts up, if this is the first time it starts up it outputs an alert saying welcome. To tell if this is first time it has opened it reads the UserDefaults and checks.

Example 2: It says, "Hello [Name]", where Name is something you have entered. If you have opened the app and there is no name, it should say "Hello World." I need to check if you have entered a name already and act accordingly. The name would be stored in NSUserDefaults.

Some help here? I'd really appreciated it!!

A: 

Ask for the object/value/whatever and if it comes back nil or zero, it isn't there.

Azeem.Butt
How exactly would I go about doing that? I know how to pull out integers or strings, but if I pulled out something that wasn't there, how would I check it?int playerOneLifeTemp = [prefs integerForKey:@"playerOneLife"];if (playerOneLifeTemp == Nil) {Do something?}
Wayfarer
If you need help learning basic C then that's another thread entirely.
Azeem.Butt
Thanks for your sarcasm, I got it figured out.
Wayfarer
You are a god among men, sir.
Azeem.Butt
It doesn't sound like you know the answer to the question very well, and are just guessing.
alexy13
+10  A: 

objectForKey will return nil if it doesn't exist

jspcal
A: 

Ask for the object/value/whatever and if it comes back nil or zero, it isn't there

What if the value saved/loaded really IS a value of zero?

It's better to check for nil... than zero.

Bonnie