2011-10-13 44 views
0

我的字符串比较一直返回false,我不明白为什么。即使我的nslog说价值是正确的。你知道为什么即使字符串看起来是相同的,我的比较仍然返回错误吗?如果我通过程序类型显示SV作为其值。我确保这个字符串中没有空格。我们得到的这前两个字符: SV2B799E5B-4306-4965-B5DD-944D3970E6B6NSString子串不会比较

NSString *fPath = [path stringByAppendingPathComponent:[directoryContent objectAtIndex:x]]; 
    NSString *fName = [directoryContent objectAtIndex:x]; 
    NSString *type = [fName substringToIndex:2]; 

    NSLog(@"TYPE: %@",type); 

    if ([type caseInsensitiveCompare:@"SV"]) 
    { 
     NSData *file = [[NSData alloc] initWithContentsOfFile:fPath]; 

     if (file) 
     { 
      [[WebService sharedWebService]saveVolunteer:nil :YES :[directoryContent objectAtIndex:x] :file]; 
      [file release]; 
     } 
    } 

回答

4

[NSString -caseInsensitiveCompare:]不返回BOOL,它返回NSComparisonResult。如果字符串相等(以不区分大小写的方式),这将会是0,这就是为什么你看到了这个结果。

反转你的结果,你会被设置,或者是更正确的,检查是否是== NSOrderedSame

2

的方法你调用返回一个NSComparisonResult,而不是一个布尔值。恰巧,一个相等的NSComparisonResult的值为零,将其解释为false。

0

caseInsensitiveCompare:返回一个NSComparisonResult。尝试[类型caseInsensitiveCompare:@“SV”] == NSOrderedSame