字符串比较 Objective-C

String compare Objective-C(字符串比较 Objective-C)
本文介绍了字符串比较 Objective-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力进行简单的比较,但我无法让它发挥作用.我正在读取一个 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to stop UIBarButtonItem text from truncating?(如何阻止UIBarButtonItem文本被截断?)
java.lang.IllegalStateException: SimpleTypeImpl should not be created for error type(异常:不应为错误类型创建SimpleTypeImpl)
Android IllegalArgumentException: The tag for fragment_XXX is invalid. Received: layout-sw600dp/fragment_XXX_0(Android IlLegalArgumentException:Fragment_XXX的标签无效。收到:Layout-sw600dp/Fragment_XXX_0)
NSURLSessionTaskPriority seems to be ignored?(NSURLSessionTaskPriority似乎被忽略了?)
How to make dataWithEPSInsideRect vector rather than bitmap in vector format?(如何用EPSInside Rect将dataWithEPSInside Rect变成矢量而不是位图的矢量格式?)
HTTPS request using volley(使用 volley 的 HTTPS 请求)