字符串和解析XML的问题

时间:2011-07-01 14:58:33

标签: iphone objective-c xml xcode string

我有一个不起作用的逻辑条件。它会比较两个字符,一个取自xml,另一个是我自己插入的。

让你知道我附在代码

下面
NSString *telefono = [NSString stringWithFormat:@"%@", [[arrayColonnine objectAtIndex:indexPath.row]objectForKey:@"telefono"]];
NSString *trimmedTelefono = [telefono stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        senzaTelefono = [[NSString alloc] initWithString:[trimmedTelefono stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];



if (telefono == @"NO")
if (trimmedTelefono == @"NO")
if (senzaTelefono == @"NO")

对于三个字符串中的每一个如果我做一个日志它打印NO。但在这三种情况中都没有。我该如何解决这个问题呢?

解决方案

这个过程就是这样:

NSString *telefono = [NSString stringWithFormat:@"%@", [[arrayColonnine objectAtIndex:indexPath.row]objectForKey:@"telefono"]];
NSString *trimmedTelefono = [telefono stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

if ([trimmedTelefono isEqualToString:@"NO"]) {
 //do something...
}

2 个答案:

答案 0 :(得分:3)

你应该使用isEqualToString:

    if ([telefono isEqualToString:@"NO"])

if (telefono == @"NO")比较相同的对象,而不是内容相同。

答案 1 :(得分:0)

不要使用==来比较NSString对象。