字符串比较目标C. [英] String compare Objective C
问题描述
我一直在努力与一个简单的比较,但我不能得到它的工作。
我在读一个XML文件,我需要比较它的数据,以显示正确的图片。
Ive been struggling with a simple comparison but i cant 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 .
http://www.cleaner.se/larm.xml (用于解析的示例文件)
http://www.cleaner.se/larm.xml (Example file for parsing)
我已经尝试过:
if([aLarm.larmClass isEqualToString:@"A"])
NSLog(@"same");
else
NSLog(@"Not same");
如果我使用: NSLog(aLarm.larmClass);
console放好它应该。我做错了什么?
If I use : NSLog(aLarm.larmClass);
console puts it out nicely as it should. What am I doing wrong?
推荐答案
您可以使用NSString compare:方法。例如:
You can use the NSString compare: methods. For example:
if ([myString caseInsensitiveCompare:@"A"] == NSOrderedSame ) {
NSLog(@"The same");
} else {
NSLog(@"Not the same.");
}
结果是一个NSComparisonResult,它只是一个枚举类型NSOrderedSame,NSOrderedAscending NSOrderedDescending。
The result is an NSComparisonResult which is just an enum with types NSOrderedSame, NSOrderedAscending and NSOrderedDescending.
检查各种compare:方法的文档这里。
Check the documentation on the various compare: methods here.
当然,如果接收者实际上是一个NSString,那么isEqualToString:也应该工作。所以如果你想比较一个类名(aLarm.larmClass ??),那么你可以调用:
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");
}
这篇关于字符串比较目标C.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!