iphone - NSString 子串不会比较

标签 iphone objective-c ios ipad

我的字符串比较总是返回错误,我不明白为什么。甚至我的 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/

相关文章:

objective-c - 设置事件指示器直到从服务器加载图像?

ios - NSDateFormatter 提前一天

javascript - React-native 和 Firebase ListView 集成

ios - 如何使 Swift 协议(protocol)符合特定种类的类?

iOS 崩溃报告服务

iphone - 使用 Xcode 查找错误 - 尝试插入 nil 值

ios - 使用autolayout在iphone 5中实现iphone 4布局

objective-c - 为什么一个空的 tableView 会检查节的数量而非空的不检查?

iPhone MapView 中断

iPhone SDK 音频混合器