无法从NSXMLParser解码UTF8字符

3
我使用XML解析器从服务器获取XML文件。
我有以下两个东西:
NSMutableArray *catalogueList;
Catalogue *currentCatalogue;

目录看起来像这样:

#import "Catalogue.h"

@implementation Catalogue

@synthesize id_model;
@synthesize name;
@synthesize url;

@end

这段代码也是如此:

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{    
 if ([elementName isEqualToString:@"catalogue"]) 
{       
    // Add currentCatalogue to array
    [catalogueList addObject: currentCatalogue.url];
}

这很好用,但是有一个小问题。当我这样做时:
 NSLog(@"Valoare url %@", currentCatalogue.url);

它会显示正确的URL。

但是当我执行以下操作时:

 NSLog(@"Valoare url %@", catalogueList);

输出结果是“link”。 这完全不正确:我得到的是“Coup\U00e9”,而不是“Coupé”。请告诉我如何在catalogueList中获取带有é字符的正确链接。

1
这看起来更像是iOS而不是Android :) - Luke
这是 iPhone。看标签。我纠正了标题。抱歉。 - adrian
3个回答

2

请确保你的url属性是数组中的字符串。

@interface Catalogue : NSObject{

NSString *myURL;

}

NSString不会改变特殊字符,并且应该输出与输入完全相同的内容。以下假设您已将字符串添加到数组中。
NSLog(@"My URL: %@", [currentCatalogue objectAtIndex:0]);

Ouput: My URL: http://host_server/uploads/modeles_pdf/41_TTRS_Coupé_TTRS_Roadster_Tarifs_20110428.pdf

此外,尝试这样做以查看是否出现相同的错误...
NSString *temp = currentCatalogue.url;
//Check the string
NSLog(@"NSString temp: %@", temp);

//Add it to the array
[catalogueList addObject:temp];

//Assuming object is at index 0, check the object in the array
NSLog(@"My URL: %@", [catalogueList objectAtIndex:0]);

1
你正在打印一个数组而不是对象。尝试这样做:
NSLog(@"catalogueList url %@", [catalogueList objectAtIndex:0]);

看看这个SOF问题NSLog不正确的编码。 UTF8字符有类似的问题


它显示了这个<Catalogue: 0x6d3b3c0>。 - adrian
你的 url 变量是一个字符串,对吧?在我的端上它完美地工作了。 - tipycalFlow
请替换我的代码,而不是使用 NSLog(@"Valoare url %@", catalogueList); - tipycalFlow
伙计,不要打印 catalogueListNSLog 中,而是打印 [catalogueList objectAtIndex:0] - tipycalFlow
让我们在聊天中继续这个讨论。 链接 - adrian
兄弟,我告诉过你我试过了,结果是:<目录:0x6d973c0>。 - adrian

0
获取UTF8字符串值并使用它。
[currentCatalogue.url UTF8String];

我尝试了一下,得到了这个错误信息:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM UTF8String]: unrecognized selector sent to instance 0x7a39d。我的应用在那行代码崩溃了! - adrian
抱歉,我的错 - 变量名打错了。想法是从服务器获取的字符串中获取UTF8String。我编辑了我的答案... - sElanthiraiyan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接