为什么NSString有时与等号一起工作? [英] Why does NSString sometimes work with the equal sign?

查看:97
本文介绍了为什么NSString有时与等号一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
了解Objective-C中的NSString比较

Possible Duplicate:
Understanding NSString comparison in Objective-C

刚读完有关相等与身份的文章时,我意识到在比较objc代码中的字符串时,我一直在使用一些等号.奇怪的是,它确实不时地起作用,我想知道为什么.

Was just reading up about equality vs identity and I realized that I've been using some equal signs when comparing strings in my objc code. The weird thing is that it actually works from times to times and I was wondering why.

http://www.karlkraft. com/index.php/2008/01/07/equality-vs-identity/

我有两段代码,一份工作,一部没有.

I have two pieces of code, one work and one doesn't.

工作.在这里,我有一个名为"Category"的对象,该对象具有一个名为"name"的NSString属性.

WORKING. Here I have a object called 'Category' which has a NSString property called 'name'.

@property (nonatomic, retain) NSString *name;

然后我有了一个传递类别"指针的函数,当我在此处使用等号时,它可以完美运行.

I then have a function where I pass in a 'Catergory' pointer and when I use the equal sign here, it works perfectly.

 -(void)addCategoryToStorage:(Category*)newcategory {
      if(newcategory.name != @"All") {  // this works

不工作.在这里,我使用NSFileManager调用的输出;

NON WORKING. Here I use the output from a NSFileManager call;

NSArray *dirContent = [self.fileManager 
                       contentsOfDirectoryAtPath:@"MyFiles"
                       error:nil];

然后我尝试比较数组中的NSString:

I then try to compare on of the NSStrings in the array:

for(int i = 0; i < [dirContent count]; i++) { 
    if([dirContent objectAtIndex:i] == @"MyFile") // This will never be True

所以我的问题是,这两个NSString指针的工作原理如此不同是什么原因?我知道我现在不应该使用"==",但是我很好奇为什么有时它会起作用.

So my questions is, what would be the reason why these two NSString pointers would work so differently? I know I shouldn't use "==" but at the moment, but I'm just so curious why it works sometimes.

我注意到它们之间的调试有所不同.在第一种情况下,"newcategory"的名称"表示(__NSCFConstantString *),并且具有可见值. 由于某些原因,"dirContent"数组包含两个(__NSString *)对象,其中的值不可见.

I noticed some difference between them debugging. In the first case, the "newcategory"s "name" said (__NSCFConstantString*) and had the value visible. For some reason, the "dirContent" arrays contains two (__NSString*) objects where the values aren't visible.

这提出了另一个问题.是什么使NSString在第一种情况下成为常量?当我将其传递给函数时,它似乎与创建方式有关.当我分配一个带有NSString属性的新对象,然后将其传递给函数时,该函数将其视为常量(对象的NSString属性).但是,当对象存储在列表中并且我通过将[array objectAtIndex:index]传递给函数来抓取时,函数不会将其作为常量(即对象的NSString属性)来获取.

This raises another question. What makes the NSString in the first case to become a constant? It seems to have something to with how it's created when I pass it to the function. When I allocate a new object with a NSString property that I then pass in to a function, the function sees it a constant(The NSString property of the object). But when the object is stored in a list and I grab it by passing [array objectAtIndex:index] into a function, the functions doesn't get it as a constant (the NSString property of the object that is).

谢谢!

推荐答案

当指针指向同一个对象时,它可能会起作用.

It may work, when the pointers point to the same object.

如果您的代码中有字符串文字,则它实际上是类型为NSString的静态分配的对象.在代码中的任何时候,如果您要引用此字符串值,则实际上是在使用相同的指针. (指针基本上是一个内存地址)

If you have a string literal in your code, it is actually a statically allocated object of type NSString. At any point in your code, if you would reference this string value, you are actually using the same pointer. (A pointer is basically a memory address)

如果在代码中多于一个点具有字符串文字@"abc",则编译器仅放入一个实例,并使用指向该单个实例的指针.

If you have the string literal @"abc" at more than one point in your code, the compiler only puts in one instance, and uses a pointer to this single instance.

这说明了为什么@"abc" == @"abc".

以您的示例为例:

if(newcategory.name != @"All")

仅在您在代码中的某些时候做过:

will only work if at some point in your code you did:

newcategory.name = @"All";

如果您愿意的话,不会起作用:

And will not work if you would do:

newcategory.name = [NSString stringWithFormat:@"%c%c%c",'A','l','l'];

因为在最后一种情况下,您显式分配了一个新字符串.

because in that last case you explicitly allocate a new string.

编辑刚刚进行了测试,该示例存在缺陷:

edit just tested, this example was flawed:

newcategory.name = [NSString stringWithString:@"All"];

因为已对其进行了优化,所以您 将具有与@"All"

because this is optimized away so you will have the same pointer to @"All"

这篇关于为什么NSString有时与等号一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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