我在做一个练习来学习如何在Objective-C中使用选择器。
在这段代码中,我试图比较两个字符串:通过选择器比较两个字符串:意外的结果
int main (int argc, const char * argv[])
{
@autoreleasepool
{
SEL selector= @selector(caseInsensitiveCompare:);
NSString* [email protected]"hello";
NSString* [email protected]"hello";
id result=[str1 performSelector: selector withObject: str2];
NSLog(@"%d",[result boolValue]);
}
return 0;
}
但它打印zero.Why?
编辑:
如果我将str2更改为@“hell”,我得到一个EXC_BAD_ACCESS。
尼斯answer.Just一个问题:是正常的,它可能会返回18446744073709551615(比较@“你好”和@“地狱”)? – 2012-07-13 21:23:52
不,这是不正常的,你是如何得到这个数字?你复制并粘贴了我的代码,然后将其更改为'hell'?我得到的唯一值是'1','0'和'-1'。 – Joe 2012-07-13 21:25:29
相同的代码,但只是它的格式错误:我写了%lu而不是%d(修复xcode警告)。将它更改为%ld并且它可以正常工作。谢谢。 – 2012-07-13 21:33:24