 |
 |
NSUserDefaults already existing keys
|
 |
|
 |
|
Mac Elite
Join Date: Jan 2001
Location: New York
Status:
Offline
|
|
How can you find out if a key exists?
Thanks.
------------------
Think Different.
|
|
|
| |
|
|
|
 |
|
 |
|
Admin Emeritus 
Join Date: Oct 2000
Location: Boston, MA
Status:
Offline
|
|
If you always have an object attached to the key, you could just check for objectForKey being not nil.
|
|
"Against stupidity, the gods themselves contend in vain" (Schiller)
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Jan 2001
Location: New York
Status:
Offline
|
|
That returned a sigsegv 11.
|
|
|
| |
|
|
|
 |
|
 |
|
Admin Emeritus 
Join Date: Oct 2000
Location: Boston, MA
Status:
Offline
|
|
You know that the key's not there 
|
|
"Against stupidity, the gods themselves contend in vain" (Schiller)
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Jan 2001
Location: New York
Status:
Offline
|
|
Yeah but I don't want my application crashing! I need to be able to check if it's there or not.
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Sep 2000
Status:
Offline
|
|
Calling objectForKey: is the right approach, something else must be busted. You're doing the following?
[[NSUserDefaults standardUserDefaults] objectForKey:defaultName]
Ali
|
|
|
| |
|
|
|
 |
|
 |
|
Admin Emeritus 
Join Date: Oct 2000
Location: Boston, MA
Status:
Offline
|
|
Oh, you need it *not* to crash :-)
I hoped that would work. Perhaps using dictionaryRepresentation (Or whatever the dictionary method is), then using objectForKey on the dictionary would stop your problem.
That is, unless you're implementing objectForKey or your Defaults incorrectly.
|
|
"Against stupidity, the gods themselves contend in vain" (Schiller)
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Jan 2001
Location: New York
Status:
Offline
|
|
I am using the
[[NSUserDefaults standardUserDefaults] objectForKey:defaultName]
and I tried the
dictionaryRepresentation
thing.
Bottom line - I'm going to just use a file for preferences instead of NSUserDefaults
------------------
Think Different.
|
|
|
| |
|
|
|
 |
|
 |
|
Admin Emeritus 
Join Date: Oct 2000
Location: Boston, MA
Status:
Offline
|
|
Ack! No! Please don't do that.
If we can figure out what's wrong, we'll *all* learn something.
Let me try to test this.
|
|
"Against stupidity, the gods themselves contend in vain" (Schiller)
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Jan 2001
Location: New York
Status:
Offline
|
|
My saving to a file thing works!
Sorry, can't waste any more time on this.
------------------
Think Different.
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Feb 2001
Location: Portland, OR, USA
Status:
Offline
|
|
Originally posted by davecom:
I am using the [[NSUserDefaults standardUserDefaults] objectForKey:defaultName]
If this crashed on you it is because your defaultName variable is invalid. It hasn't been initialized, it isn't an NSString, or the string has already been deallocated.
parallax's answer was correct: if the call returns nil, the key doesn't exist.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status:
Offline
|
|
Easy. Register a default key in the Application domain. You'd so something like:
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObject:@"-1" forKey:@"myKey"]];
Then when you check the value of the key if it is -1 then you know that it hasn't been used. Just pick a value that you know you won't use as the default. When the user sets a value and you actually need to make a key use the setObject: forKey: method.
BTW, the register defaults method registers the setting in the application domain which lives in RAM so when the user quits the setting is gone. The setObject: forKey: method corresponds to a preference file so the setting will live on the next time you launch the program.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|