函数的返回值错误(可能错误的字符串“type”?)

时间:2011-11-08 11:01:36

标签: ios xcode string function

我有一个应该返回int值的函数。

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

我在这里调用函数:

[self function:self.detailItem.type]

调试器显示input __NSCFString * 0x6b9a0b0并返回任何29938idontknow值。

如果我致电[self function:@"test1"],一切正常。

detailItemTV的类型,NSManagedObject,其属性type定义为string。应该是string - 类型的问题吗?

谢谢!

2 个答案:

答案 0 :(得分:4)

您应该像这样比较NSString:

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

答案 1 :(得分:1)

检查self.detailItem.type是否为NSString:

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

之前打电话

[self function:self.detailItem.type]

并比较像这样的字符串

[input isEqualToString:@"test1"];

您目前正在比较内存地址,在这种情况下总是不同,您应该比较它们包含的字符串。