Objective-C常数:使用==的NSString比较 [英] Objective-C constants: NSString comparison using ==?

查看:55
本文介绍了Objective-C常数:使用==的NSString比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现有关设置NSString常量的讨论使我可以通过以下方式进行编码:

The discussions I found about setting NSString constants made me code it the following way:

.h文件:

extern NSString * const kSectionHeaders;

.m文件:

NSString * const kSectionHeaders = @"header";

程序运行时,必须针对一系列NSString常量测试文本文件中的单词.

As the program runs, it has to test words from a text file against a series of NSString constants.

在设置上述功能时,我读取的内存比较应该起作用:

I read memory comparison should work when setting function like stated above:

if (property == kSectionHeaders) {...}

不努力工作:( 以下工作有效,但被描述为一个不好的解决方案(速度较慢,还有什么?):

Doesn't work tough :( The following works, but it was described as a bad solution (slower, what else?):

if ([property isEqualToString:kSectionHeaders]){...}

我觉得我做错了什么.但是看不到什么! 请帮忙 :-) 谢谢!

I feel I've done something wrong. But can't see what! Please help :-) Thanks! J.

推荐答案

记住变量名只是指向内存中对象的指针.

Remember variable names are just pointers to objects in memory.

==操作数比较指针.除非它正在比较内存中完全相同的对象,否则它不会是真的.

The == operand compares the pointers. It will not be true unless it is comparing the exact same object in memory.

isEqualToString:是您最好的选择.不必太担心速度.这些设备足够快,可以在眨眼之间进行比较.真正需要花费大量时间的事情是在屏幕上绘制并从磁盘读取.

isEqualToString: is your best bet. Don't worry too much about speed. The devices are plenty fast enough to do the comparison in the blink of an eye. The things that really take noticible time are drawing on screen and reading from disk.

这篇关于Objective-C常数:使用==的NSString比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆