在Objective-C中,方法的绑定是否真的发生在“运行时”? [英] In Objective-C, does the binding of method really happen at "run-time"?
问题描述
Objective-C,像Smalltalk一样,可以使用动态输入:一个对象可以是
发送一条未在其界面。这可以允许
增加灵活性,因为它允许对象捕获一个
消息,并将消息发送到可以适当响应
的另一个对象,或者同样发送消息到另一个
对象。
我感觉到像 [anObject someMethod] / code>,绑定
someMethod
到机器代码可能会在运行时发生。
因此,我写了一个这样的演示:
#import< Foundation / Foundation.h>
@interface Person:NSObject {
@private char * name;
}
@property(readwrite,assign)char * name;
- (void)sayHello;
@end
@implementation Person
@synthesize name;
- (void)sayHello {
printf(你好,我的名字是%s!\\\
,[self name]);
}
@end
int main(){
Person * brad = [Person new];
brad.name =Brad Cox;
[brad sayHello];
[brad sayHelloTest];
}
我尝试过 [brad sayHelloTest]
发送 brad
一条消息 sayHelloTest
哪个 brad
不知道如何处理..我预计在编译时不会发生错误..
然而,编译器仍然抛出错误:
main.m:24:11:error:实例方法'-sayHelloTest'未找到(返回类型默认为'id')[-Werror,-Wobjc-method-access]
[brad sayHelloTest];
^ ~~~~~~~~~~~
main.m:3:12:note:receiver是这里声明的类的实例
@interface Person:NSObject {
^
更改 [(id)brad sayHelloTest]
到 [(id)brad sayHelloTest];
不工作..(编译命令是 clang -Wall -Werror -g - v main.m -lobjc -framework Foundation -o main
)
在Objective-C中,方法的绑定是否真的发生在run -时间?如果是这样,为什么会有这样的编译器错误?
如果绑定没有发生在运行时,为什么Objective-C被称为动态打字语言?
有没有人对此有任何想法?
/ p>
在编译过程中抛出错误,因为包含 -Werror
标志,这将会变为警告进入错误 ..
http://clang.llvm.org/docs/UsersManual.html#cmdoption-Werror
删除 -Werror
标志,一切正常工作,错误只在运行时发生。
I heard that Objective-C is influenced by the "message passing mechanism" of SmallTalk.
Objective-C, like Smalltalk, can use dynamic typing: an object can be sent a message that is not specified in its interface. This can allow for increased flexibility, as it allows an object to "capture" a message and send the message to a different object that can respond to the message appropriately, or likewise send the message on to another object.
And I felt for codes like [anObject someMethod]
, the binding of someMethod
to the machine code may happen at run-time..
Therefore, I write a demo like this:
#import <Foundation/Foundation.h>
@interface Person : NSObject {
@private char *name;
}
@property (readwrite, assign) char *name;
- (void)sayHello;
@end
@implementation Person
@synthesize name;
- (void)sayHello {
printf("Hello, my name is %s!\n", [self name]);
}
@end
int main() {
Person *brad = [Person new];
brad.name = "Brad Cox";
[brad sayHello];
[brad sayHelloTest];
}
I tried [brad sayHelloTest]
to send brad
a message sayHelloTest
which brad
doesn't know how to handle with.. I expect the error will NOT happen at compile-time..
However, the compiler still throws an error:
main.m:24:11: error: instance method '-sayHelloTest' not found (return type defaults to 'id') [-Werror,-Wobjc-method-access]
[brad sayHelloTest];
^~~~~~~~~~~~
main.m:3:12: note: receiver is instance of class declared here
@interface Person : NSObject {
^
Change [(id)brad sayHelloTest]
to [(id)brad sayHelloTest];
doesn't work either.. (The compiling command is clang -Wall -Werror -g -v main.m -lobjc -framework Foundation -o main
)
In Objective-C, does the binding of method really happen at "run-time"? If so, why will there be a compiler error like this?
If the binding doesn't happen at "run-time", why was "Objective-C" called "dynamic typing language"?
Does anyone have any ideas about this?
I figured out the reason finally..
It throw errors during compiling because -Werror
flag is included, which will turn warning into error..
http://clang.llvm.org/docs/UsersManual.html#cmdoption-Werror
After I delete -Werror
flag, everything works as expected and the error only happens at run-time.
这篇关于在Objective-C中,方法的绑定是否真的发生在“运行时”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!