tags:

views:

66

answers:

1

Hi..

I have a language file downloaded from a web service located in my resources folder of the project. The file name is "no.properties" and I need to load the file into a string to make further use of it..

The content of my file is displayed like this:

CMD_EXIT = Avslutt

CMD_PAUSE = Pause

CMD_SCAN = Skann

ect.....

Here is my code of loading the file into a string:

NSString* path = [[NSBundle mainBundle] pathForResource:[defaults objectForKey:@"language"]  
                                                 ofType:@"properties"];

NSString* content = [NSString stringWithContentsOfFile:path 
                                              encoding:NSUTF8StringEncoding 
                                                 error:NULL];

The problem is that the "content" string is null even though the file contains a lot of data. Anyone know what might be the problem?

note: I´ve checked that the value received from userdefaults equals "no". I have also tried to rename the file to "no.txt", but still the "content" string is null.

+1  A: 

First, you could try to pass a NSError object to stringWithContentsOfFile:::.
You should receive a Cocoa error number, that (hopefully) tells you more about the problem.

NSError* err = nil;
NSString* string = [NSString stringWithContentsOfFile:path encoding:NSUTF16BigEndianStringEncoding error:&err];

As you seem to work with language files, I suspect that you are specifying the wrong encoding when reading in the strings.

weichsel
You were right, the problem was the encoding. Switched to NSASCIIStringEncoding and got the result I wanted ;) thanks!
Madoc
NSASCIIStringEncoding might work for now, but when dealing with internationalization you should save your file using UTF-16 encoding. Some sample code: http://developer.apple.com/iphone/library/samplecode/MoveMe/index.html
weichsel
okey, thanks for the tip :)
Madoc