idoodler
idoodler

Reputation: 3545

Get text of .strings file

I am trying to display the text of a .strings file in a UITextView. This is my code:

self.textview.text = [[NSString alloc] initWithContentsOfFile:self.txtPath encoding:NSUTF8StringEncoding error:nil];

but it appears that the encoding is wrong, because every time I the UITextView is blank, no text is in it. But it works with .txt, .log and other files. So how can I get the text of a .strings file?

enter image description here

Upvotes: 0

Views: 334

Answers (1)

rmaddy
rmaddy

Reputation: 318884

The .strings files tend to be created with the NSUTF16LittleEndianStringEncoding encoding, not UTF-8.

BTW - you can easily see the encoding of any file in your Xcode project. Bring up the right pane in Xcode. Then select a file in the project tree. In the right pane, look for the "Text Settings" section. It has a "Text Encoding" entry.

One other thing. Make it easier to solve such a problem. Break up the code for easier debugging and use the errorparameter.

NSError *error = nil;
NSString *text = [[NSString alloc] initWithContentOfFile:self.txtPath encoding:NSUTF8StringEncoding error:&error];
if (text) {
    self.textview.text = text;
} else {
    NSLog(@"Unable to load text from %@: %@", self.txtPath, error);
}

Upvotes: 1

Related Questions