Quote:
Originally Posted by mistergreen2011
Ok, I'm really confused with this one.
I have a NSMutable array it looks like this.
Code:
{
Fe = "0.1";
name = "DTPA Fe (10%)";
target = Fe;
tsp = 4290;
}........
I want to grab the objectKey name and do some comparisons.
Code:
//shared.elements is above
NSArray *temp = [[shared.elements objectAtIndex:i] allKeys];
// temp gives me Fe,name,target,tsp
for(int j=0; j < [temp count]; j++) {
//making sure it's a string!!!
if ([[temp objectAtIndex:j] isKindOfClass:[NSString class]]) {
NSLog(@"yes i'm a string");
}
if([(NSString *)[temp objectAtIndex:j] lowercaseString] == @"name") {
NSLog(@"expletive");
// nothing
}
if([temp objectAtIndex:j] == @"name") {
NSLog(@"expletive");
// nothing
}
}
None of the conditions are true except that it's a confirmation it's a NSString.
confused.
|
You can't use == to compare objects. (NSStrings are objects).
In Objective C, a variable that refers to an instance of an object is actually a pointer.
So when you say
Code:
MyObject *anObject = [MyObject alloc] init];
the "anObject" variable is actually a pointer to the memory address of an object of type MyObject.
When you use == to compare 2 objects, you are actually comparing 2 pointer variables. The only time
Code:
if (objectA == objectB)
will evaluate as TRUE is if the variables objectA and objectB point to the same address.
To compare strings, use code like this:
Code:
if ([string1 isEqualToString: @"string"])
{
//strings are equal, do something
}
Note that you have the same problem in trying to compare 2 C strings.
If you have code like this
Code:
char *aString = "foo";
char *bString = "foo";
if (aString == bString)
{
//won't work
}
Will not work, because the if statement compares the addresses of aString and bString,
not the string contents.
To compare 2 C strings, you use the strcmp function:
Code:
char *aString = "foo";
char *bString = "foo";
if (strcmp(aString,bString))
{
//C strings are equal
}