Wrong return value of function (Probably wrong string "type"?)

324 views Asked by At

I got a function which should return an intvalue.

- (int)function:(NSString *)input
{
 if (input == @"test1")
    {
        return 0;  
    }
 if (input == @"test2")
    {
        return 1;  
    }
 if (input == @"test3")
    {
        return 2;  
    }
 else
{
    return 3;
}
}

Here I call the function:

[self function:self.detailItem.type]

The debugger shows input __NSCFString * 0x6b9a0b0 and returns any 29938idontknowvalue.

If I call [self function:@"test1"] everything works fine.

The detailItemis type of TVwhich is a NSManagedObjectwith the attribute type defined as string. Should be a problem with the string-types?

Thank you!

2

There are 2 answers

0
NSZombie On BEST ANSWER

You should compare NSString like this :

if ([input isEqualToString:@"test1"])
{
    // Some code here
}
0
jbat100 On

Check that self.detailItem.type is an NSString:

if ([self.detailItem.type isKindOfClass:[NSString class]])

before calling

[self function:self.detailItem.type]

and compare strings like this

[input isEqualToString:@"test1"];

you are currently comparing memory addresses, which in this case will be always different, you should be comparing the strings they contain.