Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > NSUserDefaults already existing keys

NSUserDefaults already existing keys
Thread Tools
Mac Elite
Join Date: Jan 2001
Location: New York
Status: Offline
Reply With Quote
Apr 10, 2001, 11:49 PM
 
How can you find out if a key exists?
Thanks.

------------------
Think Different.
     
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Apr 11, 2001, 10:00 AM
 
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)
     
davecom  (op)
Mac Elite
Join Date: Jan 2001
Location: New York
Status: Offline
Reply With Quote
Apr 11, 2001, 03:44 PM
 
That returned a sigsegv 11.
     
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Apr 11, 2001, 04:06 PM
 
You know that the key's not there
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
davecom  (op)
Mac Elite
Join Date: Jan 2001
Location: New York
Status: Offline
Reply With Quote
Apr 11, 2001, 04:18 PM
 
Yeah but I don't want my application crashing! I need to be able to check if it's there or not.
     
ali
Forum Regular
Join Date: Sep 2000
Status: Offline
Reply With Quote
Apr 11, 2001, 05:28 PM
 
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
Reply With Quote
Apr 11, 2001, 05:33 PM
 
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)
     
davecom  (op)
Mac Elite
Join Date: Jan 2001
Location: New York
Status: Offline
Reply With Quote
Apr 11, 2001, 06:01 PM
 
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
Reply With Quote
Apr 11, 2001, 06:39 PM
 
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)
     
davecom  (op)
Mac Elite
Join Date: Jan 2001
Location: New York
Status: Offline
Reply With Quote
Apr 11, 2001, 07:21 PM
 
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
Reply With Quote
Apr 11, 2001, 10:13 PM
 
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
Reply With Quote
Apr 12, 2001, 05:24 PM
 
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.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:02 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2