views:

37

answers:

1

I had not run into this before but I wanted to know if it's possible to store KEYS with special characters in Objective-C / Cocoa Touch

Since one can force a line break in a NSString with \n I wanted to store some values in a plist (and they do need to be the keys). It's just about 20 lines to static data (well, the keys, the values are NSNumbers that get saved to a file) for which I don't want to use sqlite, so it's either a dictionary or a static array that I can save to disk.

Here is the .plist alt text

So when I try to set a value for any key without special characters all is fine. But trying to set for, say, 'Leg Width\n or Height' returns

 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFDictionary setObject:forKey:]: attempt to insert nil key'

being the argument a NSString = @"Leg Width\n or Height"

Any help is appreciated

Best regards david

+1  A: 

The string in the plist editor is the unescaped string. So, I often prefer to keep regular expressions in the plist file, using the plist editor.

So, for the entry Leg Width\n or Height, the key to be used is

    [dict objectForKey:@"Leg Width\\n or Height"]

Note that you put a literal backslash in the plist entry.

Yuji
Or enter the line break in the plist editor by typing option-return.
Johan Kool
hi guys, thanks both for the replies. I went with Johan's solution as it's quicker to just open the plists as a plain text file and hitting return than going back to my code and replace all call to the function that saves the dictionary so that the scape string argument becomes unescaped
dhomes