String compare Objective-C

前端 未结 2 455
说谎
说谎 2021-01-17 01:08

I\'ve been struggling with a simple comparison but I can\'t get it to work. I´m reading a XML file and I need to compare data from it in order to show the right picture.

2条回答
  •  囚心锁ツ
    2021-01-17 02:03

    You can use the NSString compare: methods. For example:

    if ([myString caseInsensitiveCompare:@"A"] == NSOrderedSame ) {
        NSLog(@"The same");
    } else {
        NSLog(@"Not the same.");
    }
    

    The result is an NSComparisonResult which is just an enum with types NSOrderedSame, NSOrderedAscending and NSOrderedDescending.

    Check the documentation on the various compare: methods here.

    Of course, if the receiver is actually an NSString, then isEqualToString: should also work. So if you're trying to compare a class name (aLarm.larmClass ??), then you can call:

    if ([NSStringFromClass([aLarm class]) isEqualToString:@"A"] ) {
        NSLog(@"The same");
    }
    

提交回复
热议问题