Objective-C如何提供动态运行时? [英] How does Objective-C provide a dynamic runtime?

查看:160
本文介绍了Objective-C如何提供动态运行时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Objective-C如何提供动态运行时? 动态是指什么?

How does Objective-C provide a "dynamic" runtime? What does "dynamic" refer to here?

推荐答案

在一句话中:Objective-C决定哪种方法 em>在这样做之前(在运行期间)调用。这个想法是,方法的名称实现之间的连接是动态的。 C ++例如在编译期间这样做。

In one sentence: Objective-C decides which method implementation to call right before doing so (during runtime). The idea is that the connection between the name of a method and the implementation is dynamic. C++ for example does this during compile time.

示例:

id object = @"1";
int i = [object intValue];

object = @1;
i = [object intValue];

在此示例中, intValue 消息是第一个发送到 NSString 的实例,然后发送到 NSNumber 。编译器发出的代码对于两个调用是相同的 - 实际上,编译器甚至不知道向哪个类型的对象发送消息(类型是 id )。

In this example the intValue message is first sent to an instance of NSString and then to an NSNumber. The code emitted by the compiler is identical for both calls–in fact the compiler does not even know to which kind of object it's sending messages to (as the type is id).

运行时决定要调用哪个实现来从字符串或 NSNumber 中提取int值。

The runtime decides which implementation to call to extract an int value from a string or an NSNumber.

这篇关于Objective-C如何提供动态运行时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆