gpt4 book ai didi

objective-c - Objective-C中的id类型真的是动态的吗

转载 作者:搜寻专家 更新时间:2023-10-30 19:51:32 24 4
gpt4 key购买 nike

id 类型是真正动态的(在运行时后期绑定(bind))还是类似于 C# 中的“var”关键字(在编译时后期绑定(bind))?

最佳答案

来自 The Objective-C Programming Language :

id is defined as pointer to an object data structure:

typedef struct objc_object {
Class isa;
} *id;

更重要的是

The id type is completely nonrestrictive. By itself, it yields no information about an object, except that it is an object. At some point, a program typically needs to find more specific information about the objects it contains. Since the id type designator can’t supply this information to the compiler, each object has to be able to supply it at runtime.

The isa instance variable identifies the object’s class—what kind of object it is. Objects with the same behavior (methods) and the same kinds of data (instance variables) are members of the same class.

Objects are thus dynamically typed at runtime. Whenever it needs to, the runtime system can find the exact class that an object belongs to, just by asking the object. (To learn more about the runtime, see Objective-C Runtime Programming Guide.) Dynamic typing in Objective-C serves as the foundation for dynamic binding, discussed later.

关于objective-c - Objective-C中的id类型真的是动态的吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4150054/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com