String compare Objective-C

99封情书 提交于 2019-12-01 13:59:56

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");
}

If the larmClass property is a string, make sure that it is actually one character in length (i.e. it doesn't have any leading or trailing whitespace that was accidentally included when parsing the XML). If the larmClass property truly is an NSString containing the letter ‘A’ then [aLarm.larmClass isEqualToString:@"A"] will return YES.

Do a:

NSLog(@"%u, %@", [aLarm.larmClass length], aLarm.larmClass);

and just make sure that it shows “1, A”.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!