Possible Duplicate:
Understanding NSString comparison in Objective-C
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.
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
NON WORKING. Here I use the output from a NSFileManager call;
NSArray *dirContent = [self.fileManager
contentsOfDirectoryAtPath:@"MyFiles"
error:nil];
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
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.
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.
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).
Thanks!