开发者

Using å ö ä in key of NSDictionary

I a开发者_如何学Cm creating an application where a user can click a city name in a tableview. I'm identifying the name the user selected to match a key in a NSDictionary.

The problem is that some of the cities are having å ä ö in their name, since the app should work in Sweden.

The NSDictionaries are stored in a plist-file. Looks like this:

{
    Göteborg = {
        name = "Göteborg";
        adress = "xxx";
        phone = "xxx";
    };

}

How should I make this work? I can think of a few stragies, which I don't know how to implement:

  1. Escape the å ä ö in in the plist file some way. But how?

  2. In the plist, instead of Göteborg I could call the key Goteborg and in the code always exchange the å ä ö to a a o before calling the key in the dictionary. But how do I do that if I'm not going to loop through the string and build up a new one? I assume it's possible in an easy way, e.g. a "change all ö to o" method.

This is the error message I get when trying to compile the application:

English.lproj/factories.plist: Unexpected character { at line 1
Command /Developer/Library/Xcode/Plug-ins/CoreBuildTasks.xcplugin/Contents/Resources/copyplist failed with exit code 1

Thanks in advance!


NSString and plist file format do support Unicode, without any problems. You should not care about using Unicode strings in NSDictionary.

Still there are two potential issues with what you want to do.

You may need to normalize Unicode strings when you add them as keys and before checking if key is in the dictonary.

The second, you may want to be able to do partial matches inside the dictionary, for example if someone enters a name without diacritics (accents). For this I think it would be easier to add aliases inside the hash.


It's completely possible. The pasted description of the plist looks like a paste from a browser's way of displaying it. In fact, .plist files are .xml files and need to be constructed in the same way (an array of dictionaries with Apple-plist header) as MyApplicationName-Info.plist in your Resources group, and also need to be in UTF-8 (to save hassle / be loaded with f.ex. NSArray initWithContentsOf...).

If I were guessing, I think the plist is generated by a script on the web and not saved in UTF-8. Check the encoding in a text editor on Mac or in Notepad++.

It certainly is completely possible, I do this all the doo dah day. :)


Using a Hex Editor I found incorrect signs that followed when I copied from the web. Removing those characters fixed the problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜