通过选择器比较两个字符串:意外结果

Ram*_*uri 1 objective-c selector automatic-ref-counting

我正在练习如何在Objective-C中使用选择器.
在这段代码中,我试图比较两个字符串:

int main (int argc, const char * argv[])
{
    @autoreleasepool
    {
        SEL selector= @selector(caseInsensitiveCompare:);
        NSString* str1=@"hello";
        NSString* str2=@"hello";
        id result=[str1 performSelector: selector withObject: str2];
        NSLog(@"%d",[result boolValue]);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但它打印为零.为什么?

编辑:
如果我将str2更改为@"hell",我会得到一个EXC_BAD_ACCESS.

Joe*_*Joe 6

performSelector:状态的文档"对于返回除对象以外的任何内容的方法,请使用NSInvocation".因为caseInsensitiveCompare:返回一个NSInteger而不是一个对象,你需要创建一个NSInvocation更复杂的对象.

NSInteger returnVal;
SEL selector= @selector(caseInsensitiveCompare:);
NSString* str1=@"hello";
NSString* str2=@"hello";

NSMethodSignature *sig = [NSString instanceMethodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];
[invocation setTarget:str1];
[invocation setSelector:selector];
[invocation setArgument:&str2 atIndex:2]; //Index 0 and 1 are for self and _cmd
[invocation invoke];//Call the selector
[invocation getReturnValue:&returnVal];

NSLog(@"%ld", returnVal);
Run Code Online (Sandbox Code Playgroud)