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 > NSDictionary And Writing To It

NSDictionary And Writing To It
Thread Tools
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
May 3, 2004, 10:46 AM
 
Hey guys,

What I'm looking to do is on the apps first startup, write some stuff initially to my NSUserDefaults.

I can do it for simple stuff, but this seems a little more complex, basically what I'm trying to add on startup is some data to a NSTableView, when a user adds something it gets recorded fine, but I'd like to add some default values. Here is a plist snippet:

Code:
<key>SnippetList</key> <dict> <key>Snippet1</key> <dict> <key>Shortcut</key> <string>Option+Command+3</string> <key>Template</key> <string>Text</string> </dict> </dict>
So basically, I want to add like 5 default items to my NSTableView on launch, I looked up on Apple's Docs and found this code:

Code:
+ (void)initialize { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:@"YES" forKey:@"DeleteBackup"]; [defaults registerDefaults:appDefaults]; }
But I'm not sure how to write actually inside another NSDictionary. I'm probably way off here.

Thanks for any help,
Oliver
     
qyn
Dedicated MacNNer
Join Date: Dec 2000
Location: sj ca
Status: Offline
Reply With Quote
May 3, 2004, 12:34 PM
 
Originally posted by iOliverC:

Code:
+ (void)initialize { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:@"YES" forKey:@"DeleteBackup"]; [defaults registerDefaults:appDefaults]; }
But I'm not sure how to write actually inside another NSDictionary. I'm probably way off here.
No, you're doing the right thing with the above code.

You can do what the example code does (dictionaryWithObject:), or you can create an NSMutableDictionary and add the items one by one. The key part is the registerDefaults: method once the dictionary has been constructed.

The code above runs as a class method, which is not likely where you want to put it. Typically it will be in the application delegate applicationDidFinishLaunching: method, or perhaps in an awakeFromNib: method. But it all depends on your specific needs.

I think you'll be able to get more help if you post more specific information about what your prefs look like...
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
May 3, 2004, 05:39 PM
 
Originally posted by qyn:
You can do what the example code does (dictionaryWithObject, or you can create an NSMutableDictionary and add the items one by one.
Or if you'd like to stick with NSDictionary, you can also use [[NSDictionary alloc] initWithObjectsAndKeys:obj1, key1, obj2, key2, nil] or any of the other multiple-object NSDictionary initializers.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Professional Poster
Join Date: Sep 2000
Location: Texas
Status: Offline
Reply With Quote
May 3, 2004, 05:58 PM
 
I like to use the NSMutableDictionary and then write it out to file when you need to save it.
     
   
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 06:28 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