Objective-C 中 (Object*) 和 (id) 的区别
请帮助我找出这两个代码片段之间的区别: (在片段中 Foo 是从 objc/Object.h 中声明的 Object 派生的类)
// Snippet 1
Object* o = [Foo new];
[o free];
// Snippet 2
id o = [Foo new];
[o free];
谢谢!
编辑
感谢您的帮助!让我分享一个我找到的链接,也许它会帮助那些像我一样遇到同样问题并希望更好地理解它的人: id_vs_NSObject。
Help me please to find out what's the difference between these two code snippets:
(In the snippets Foo is a class derived from Object declared in objc/Object.h)
// Snippet 1
Object* o = [Foo new];
[o free];
// Snippet 2
id o = [Foo new];
[o free];
Thanks!
EDIT
Thanks for the helpful answers! Let me share a link that I found, maybe it'll help those, who meets the same question like me, and want to understand it better:
id_vs_NSObject.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
id
可以是任何内容,并且可以在没有警告的情况下响应系统中的任何消息,因为它可以是任何类型。Object *
(您的意思是NSObject *
?)是强类型的——编译器假设它只响应Object
已知会响应的方法到。id
can be anything and can respond to any message in the system without warning, as it could be of any type.Object *
(do you meanNSObject *
?) is strongly typed--the compiler assumes it only responds to methods thatObject
is known to respond to.