 |
 |
NSDictionary And Writing To It
|
 |
|
 |
|
Grizzled Veteran
Join Date: Jun 2002
Status:
Offline
|
|
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
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Dec 2000
Location: sj ca
Status:
Offline
|
|
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
|
|
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
|
|
I like to use the NSMutableDictionary and then write it out to file when you need to save it.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
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
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|