0

我正在使用以下代码阅读这本编程书

#import "Fraction.h"
int main (int argc, char * argv [])
{
    @autoreleasepool {
        Fraction *f = [[Fraction alloc] init];
        [f noSuchMethod];
        NSLog (@"Execution continues!");
    }
    return 0;
}

显然它应该给我以下输出:

* 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[Fraction noSuchMethod]:无法识别的选择器发送到实例 0x103f00”*第一次抛出调用堆栈:“”)

相反,我只是收到一条错误消息:No visible @interface for 'Fraction' declarations the selector 'noSuchMethod'

这是因为我有更新版本的 xcode,还是我做错了什么?这对我来说似乎很简单。

编辑:

另外......下面的代码可以在最新版本的xcode中工作吗?

int main (int argc, char * argv [])
{
    @autoreleasepool {
        Fraction *f = [[Fraction alloc] init];
        @try {
            [f noSuchMethod];
        }
        @catch (NSException *exception) {
            NSLog(@"Caught %@%@", [exception name], [exception reason]);
        }
        NSLog (@"Execution continues!");
    }
return 0;
}

编辑#2: 在此处输入图像描述

4

1 回答 1

2

如果你调用一个肯定不存在的方法,Xcode 不会让你编译。

否则使用该performSelector:方法。

编辑

如果你直接调用它不会编译的方法,就像你问的最初的问题一样。

如果由于某种原因您仍然想调用此方法,因为它可能是私有的或其他的,您可以通过 performSelector: 调用它。

它仍然会告诉您该对象可能不会响应它。您可以抑制警告,如您在此处看到的:

http://www.stackoverflow.com/a/7933931/1320374

于 2012-12-07T10:33:43.833 回答