开发者

UTF 8 string(russian encoding) errors, iphone

for (NSDictionary *status in statuses)
    {
        for (NSMutableString *name in [statuses objectForKey:@"response"]) 
        {

            NSMutableString *name1=[[NSMutableString alloc] initWithFormat:@"%@",name];
            const char *utf8name=[name1 UTF8String];
            NSMutableString *name_str=[[NSMutableString alloc] initWithCString:utf8name encoding:4]; 

            [list_names addObject:[NSMutableString  stringWithFormat:@"%@",name_str]];
            [name_str release];
        }
NSLog(@"%@",list_names);

(
    "{\n    \"first_name\" = \"\\开发者_如何转开发U0421\\U0442\\U0430\\U0441\";\n    \"last_name\" = \"\\U0411\\U044b\\U043a\\U043e\\U0432\";\n    uid = 41182952;\n}",
    "{\n    \"first_name\" = Victoria;\n    \"last_name\" = Violette;\n    uid = 56292826;\n}",
    "{\n    \"first_name\" = \"\\U042e\\U043b\\U044f\";\n    \"last_name\" = \"\\U0411\\U0438\\U043b\\U043e\\U0448\\U043d\\U0438\\U0447\\U0435\\U043d\\U043a\\U043e\";\n    uid = 73743149;\n}",
    "{\n    \"first_name\" = \"\\U041a\\U0430\\U0440\\U0438\\U043d\\U0430\";\n    \"last_name\" = \"\\U041f\\U0435\\U0440\\U043e\\U0432\\U0430\";\n    uid = 77001828;\n}",
    "{\n    \"first_name\" = \"\\U0415\\U043b\\U0438\\U0437\\U0430\\U0432\\U0435\\U0442\\U0430\";\n    \"last_name\" = \"\\U041b\\U0435\\U0431\\U0435\\U0434\\U0435\\U043d\\U043a\\U043e\";\n    uid = 90720663;\n}",
    "{\n    \"first_name\" = \"\\U041c\\U0430\\U0440\\U0438\\U043d\\U0430\";\n    \"last_name\" = \"\\U0414\\U0440\\U044b\\U0433\\U0430\";\n    uid = 91646824;\n}",
    "{\n    \"first_name\" = \"\\U0412\\U0438\\U043a\\U0430\";\n    \"last_name\" = \"\\U041a\\U0430\\U0440\\U0430\\U0431\\U0446\\U043e\\U0432\\U0430\";\n    uid = 96178026;\n}",
    "{\n    \"first_name\" = Pink;\n    \"last_name\" = Angel;\n    uid = 97996978;\n}",
    "{\n    \"first_name\" = \"\\U0410\\U043d\\U043d\\U0430\";\n    \"last_name\" = \"\\U0420\\U043e\\U0434\\U0438\\U043d\\U0430\";\n    uid = 133053290;\n}"
)}

English text displays normal, but russian not.How to fix it? I thinked that the problem in UTF8, but I change encoding to UTF8 and no changes.


When you do this:

NSLog(@"%@", list_names);

it calls the -description method for list_names. For an NSArray, this prints the array out as it would appear in an old text plist. The %@ format specifier is normally used for debugging purposes when it's not being used for strings.

The code converts to UTF-8 and then back to an NSString. This round trip operation serves no purpose.

The code uses [NSString stringWithFormat:@"%@", obj]. This is equivalent to calling [obj description].

The code iterates with a variable that is not used in the loop body.

Here is a quick suggestion:

for (NSDictionary *response in [status objectForKey:@"response"]) {
    NSString *firstName = [response objectForKey:@"first_name"];
    NSString *lastName = [response objectForKey:@"last_name"];
    [list_names addObject:[NSString stringWithFormat:@"%@, %@", last_name, first_name]];
}
NSLog(@"%@", list_names);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜