 |
 |
Weird NSMutableDictionary Problems...
|
 |
|
 |
|
Grizzled Veteran
Join Date: Jun 2002
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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.
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

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