2011-10-13 48 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