问题描述
我一直在努力进行简单的比较,但我无法让它发挥作用.我正在读取一个 XML 文件,我需要比较其中的数据以显示正确的图片.
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.
http://www.cleaner.se/larm.xml(示例文件解析)
我尝试过类似的方法:
if([aLarm.larmClass isEqualToString:@"A"])
NSLog(@"same");
else
NSLog(@"Not same");
如果我使用:NSLog(aLarm.larmClass);
控制台可以很好地输出它.我做错了什么?
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.
查看各种比较的文档:方法 这里.
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");
}
这篇关于字符串比较 Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!