我的字符串比较总是返回错误,我不明白为什么。甚至我的 nslog 也说该值是正确的。你知道为什么我的比较总是返回错误,即使字符串看起来是一样的吗?如果我单步执行程序类型,则显示 SV 作为其值。我已确保此字符串中也没有空格。我们得到了这个的前两个字符: SV2B799E5B-4306-4965-B5DD-944D3970E6B6
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];
}
}
最佳答案
[NSString -caseInsensitiveCompare:]
不返回 BOOL
,它返回 NSComparisonResult
。如果字符串相等(以不区分大小写的方式),这将为 0,这就是您看到该结果的原因。
反转你的结果,你就会被设置,或者为了更正确,检查它是否 == NSOrderedSame
。
关于iphone - NSString 子串不会比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7761697/