4

I am 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!

Nicsoft
  • 3,644
  • 9
  • 41
  • 70
  • 1
    Why won't that work? A key can in fact, be any kind of object and a string with special characters is also allowed. –  Jun 04 '11 at 15:23
  • 1
    what is the issue if the cities have special characters? May be you are not using the proper encoding for NSString in your string operations ... – Tatvamasi Jun 04 '11 at 15:27
  • 1
    Check this answer to "flatten" special chars: http://stackoverflow.com/questions/5217632/convert-special-characters-like-e-a-e-a-all-to-e-a-e-a-objective-c – cem Jun 04 '11 at 15:27
  • I updated my post with the error message. I have opened the file in an external text editor (TextWrangler) in order to make sure it is saved as UTF8. If it wasn't before, it is now, still with same problem. I haven't done anything encoding-stuff in my code, should that be necessary? – Nicsoft Jun 04 '11 at 17:13
  • 2
    Check the file properties inside Xcode -- does Xcode think it's in UTF-8? – spacehunt Jun 04 '11 at 20:01
  • No, xCode actually didn't. I did change them but without any change. What I ended up doing was to remove the files and added them again. With different name since xCode complained that the files had been removed when I added them with the same name. Thanks all, helped a lot. – Nicsoft Jun 05 '11 at 07:37

3 Answers3

3

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.

sorin
  • 161,544
  • 178
  • 535
  • 806
2

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. :)

Henrik Erlandsson
  • 3,797
  • 5
  • 43
  • 63
  • It's saved in UTF8. I opened it in an externa text editor and made sure I saved it in UTF8. Actually, I did copy an xml-structure to start with from the web. While editing it xCode changed the format to the one I presented. There is not XML when I open it in another text editor and the header I copied form the web is also removed. But since it works if I don't use å ä ö I guess the problem is elsewhere...or...? – Nicsoft Jun 04 '11 at 17:45
  • Here is an example of the format you should see when your dictionary is correctly stored in a file. Does yours follow this format? Else insert a cut/paste from a text editor from the plist in your question and I'll help. http://code.google.com/p/cfpropertylist/source/browse/trunk/examples/sample.xml.plist?r=5 – Henrik Erlandsson Jun 12 '11 at 00:20
0

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

Nicsoft
  • 3,644
  • 9
  • 41
  • 70