开发者

NSString substring wont compare

开发者 https://www.devze.com 2023-04-13 02:13 出处:网络
My string comparison keeps returning false and I dont understand why.Even my nslog says the value is correct.Do you know why my comparison keeps returning fa开发者_如何学运维lse even though the string

My string comparison keeps returning false and I dont understand why. Even my nslog says the value is correct. Do you know why my comparison keeps returning fa开发者_如何学运维lse even though the strings appear to be the same? If I step through the program type shows SV as its value. I have ensured there are no spaces in this string as well. We get the first two chars of this: 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:] does not return a BOOL, it returns an NSComparisonResult. This is going to be 0 if the strings are equal (in a case insensitive fashion), which is why you're seeing that result.

Invert your result and you'll be set, or to be more correct, check to see if it is == NSOrderedSame.


The method you are calling returns an NSComparisonResult, not a boolean value. It so happens that an NSComparisonResult of equal has the value zero, which is interpreted as false.


caseInsensitiveCompare: returns a NSComparisonResult. Try [type caseInsensitiveCompare:@"SV"] == NSOrderedSame

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号