且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Objective-C错误“"XYZPerson"的无可见@interface声明选择器"saySomething"

更新时间:2023-01-05 09:25:09

(从注释移至答案...)

MatthewD:如果将XYZPerson.h中的- (void) saySomething;更改为- (void) saySomething:greet;,会发生什么?

MatthewD: What happens if you change - (void) saySomething; in XYZPerson.h to - (void) saySomething:greet;?

Raj0689:为什么将其更改为saySomething:greet而不是saySomething时,它可以运行?由于只与saySomething !!

Raj0689: Why does it run when I change it to saySomething:greet and not saySomething ? Since greet is defined only along with saySomething !!

调用方法时,编译器需要找到该方法的签名,以便它可以验证该方法是否被正确调用.签名包括方法名称以及参数的数量和类型.提供方法签名的通常方法是导入定义这些签名的头文件.

When you call a method, the compiler needs to locate the signature of that method so it can verify that the method is being called correctly. The signature includes the method name and the number and types of parameters. The usual way of providing method signatures is by importing the header file that defines those signatures.

因此,请在XYZShout.m的呼叫位置:

So, in XYZShout.m where you call:

[super saySomething:upperGreet];

编译器搜索由XYZShout.m导入的XYZShout.h和由XYZShout.h导入的XYZPerson.h.在XYZShout.h中,发现了以下方法:

The compiler searches XYZShout.h, which is imported by XYZShout.m, and XYZPerson.h, which is imported by XYZShout.h. In XYZShout.h, the following method was being found:

-(void) saySomething;

这在名称上匹配被调用的方法,但在参数上不匹配,因此编译器不认为这是匹配项.在任何地方都找不到saySomething的其他定义,因此它给出了一个错误.

This matches the called method in name, but not in parameters, so the compiler does not consider this a match. No other definitions of saySomething are found anywhere, so it gives an error instead.