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 > Weird NSMutableDictionary Problems...

Weird NSMutableDictionary Problems...
Thread Tools
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Jul 17, 2004, 03:31 PM
 
Hi guys,

I'm trying to save some data to a plist installed on a users hard drive. Here is my current code to do it (does not work):

Code:
- (void)save { NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile:[NSHomeDirectory() stringByAppendingString:@"/Library/Application Support/Tag/Snippets/Snippets.plist"]]; NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; NSEnumerator *enumerator =[listOfSnippets objectEnumerator]; TagSnippet *snippet; while (snippet = [enumerator nextObject]) { NSMutableDictionary *snippetValues = [NSMutableDictionary dictionary]; [snippetValues setValue:[snippet snippetTemplate] forKey:TAG_SNIPPET_TEMPLATE_KEY]; [snippetValues setValue:[[snippet shortcutKey] stringValue] forKey:TAG_SNIPPET_SHORTCUT_KEY]; [dictionary setValue:snippetValues forKey:[snippet title]]; } [dict setObject:dictionary forKey:TAG_SNIPPETS_KEY]; }
For some reason at all, nothing is written to dict at all. But, if I replace dict with [NSUserDefaults standardUserDefaults] in the last line, it writes to my preference file fine.

The file is there (I checked), so I can't really see why it shouldn't work.

Any ideas?

Thanks
Oliver
     
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Jul 17, 2004, 04:25 PM
 
You're never saving "dict". You need [dict writeToFile: path atomically: YES] in there at the end. 'path' is the same path you got at the beginning.
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Jul 17, 2004, 04:43 PM
 
Hmm, I tried:

Code:
- (void)save { NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile:[NSHomeDirectory() stringByAppendingString:@"/Library/Application Support/Tag/Snippets/Snippets.plist"]]; NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; NSEnumerator *enumerator =[listOfSnippets objectEnumerator]; TagSnippet *snippet; while (snippet = [enumerator nextObject]) { NSMutableDictionary *snippetValues = [NSMutableDictionary dictionary]; [snippetValues setValue:[snippet snippetTemplate] forKey:TAG_SNIPPET_TEMPLATE_KEY]; [snippetValues setValue:[[snippet shortcutKey] stringValue] forKey:TAG_SNIPPET_SHORTCUT_KEY]; [dictionary setValue:snippetValues forKey:[snippet title]]; } [dict setObject:dictionary forKey:TAG_SNIPPETS_KEY]; [dict writeToFile:[NSHomeDirectory() stringByAppendingString:@"/Library/Application Support/Tag/Snippets/Snippets.plist"] atomically:YES]; }
And it still doesn't seem to save.

Any other ideas?

Much appreciated,
Oliver
     
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Jul 17, 2004, 06:11 PM
 
Turns out it was another piece of code conflicting. Reverting it to an old version.

Thanks anyway

Oliver
     
Fresh-Faced Recruit
Join Date: Oct 2000
Location: ny, ny, ny
Status: Offline
Reply With Quote
Jul 19, 2004, 06:29 PM
 
Instead of
Code:
[dict writeToFile:[NSHomeDirectory() stringByAppendingString:@"/Library/Application Support/Tag/Snippets/Snippets.plist"] atomically:YES];
You should probably use

Code:
[dict writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:@"/Library/Application Support/Tag/Snippets/Snippets.plist"] atomically:YES];
which will automatically do the right thing with leading/trailing slashes and so on.
     
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Jul 20, 2004, 07:51 AM
 
Originally posted by endian:
Instead of
Code:
[dict writeToFile:[NSHomeDirectory() stringByAppendingString:@"/Library/Application Support/Tag/Snippets/Snippets.plist"] atomically:YES];
You should probably use

Code:
[dict writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:@"/Library/Application Support/Tag/Snippets/Snippets.plist"] atomically:YES];
which will automatically do the right thing with leading/trailing slashes and so on.
Good idea, thanks
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Jul 22, 2004, 03:20 PM
 
Or even [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/Tag/Snippets/Snippets.plist"]
Or [@"~/Library/Application Support/Tag/Snippets/Snippets.plist" stringByExpandingTildeInPath]
     
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Jul 22, 2004, 06:24 PM
 
Or even better still, use Folder.h to get the "Application Support" folder in kUserDomain.
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
   
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 01:03 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