views:

120

answers:

1

I'm getting a string from a server call, and Trying to set that string to the value of the text in a UITextField. However, if the text has any special characters, the encoding gets screwed up on those characters.

-(void)contentDidLoad:(NSString *)content
{
    NSLog(@" content being put into textfield is : %@", content);
    self.textField.text = content;
    NSLog(@" content in the textfield is : %@", self.textField.text);
}

If the string is "töä", this is the output of the Logs:

content being put into textfield is : töä
content in the textfield is : töä

Anyone know whats going on?

UPDATE:

Actually, I've run into a bit of a problem with Pugal's solution. If the string contains any %'s, the string returned by calling stringByReplacingPercentEscapesUsingEncoding will be nil. To get around this, I tried calling stringByReplacingOccurancesOfString, replacing every % with %25. However, after calling that method, the encoding is messed up already! This bug is driving me crazy!

NSLog(@"Original content is: %@", contentPage.content);
NSString *text= [contentPage.content stringByReplacingOccurrencesOfString:@"%" withString:@"%25"];
NSLog(@"after % replacement, content is: %@", text);
self.textField.text = [text stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"textfield text is: %@", self.textField.text);

2010-08-04 09:43:31.742 Confluence[49621:207] Original content is: 90% of all statistics are made up always. Åöä코셔
2010-08-04 09:43:31.744 Confluence[49621:207] after  replacement, content is: 90%25 of all statistics are made up always. Ãöäì½ì
2010-08-04 09:43:31.747 Confluence[49621:207] textfield text is: 90% of all statistics are made up always. Ãöäì½ì
+1  A: 

Hi,

Try this one

Thanks.

Pugal Devan
This did it! Thank you so much!
Matthew Rohr
Found a problem with this solution, edited my question accordingly...
Matthew Rohr
In my case, server side they are encoded the values and after i decoded the string. In server side they have used "http://www.robelle.com/smugbook/ascii.html" to taken the ASCII value for the particular characters and then encoded the strings. So better you have to changed(Encode) the values in the server side else you used ASCII values for the particular characters in your code. Using "[@"Your String" stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];this code for only decoded the strings.Thanks
Pugal Devan
If you have used XML parsing. Check the details in the browser, because your case the extra symbols are there. So better you have to check your XML file in the Browser. Thanks.
Pugal Devan