开发者

Difference of (Object*) and (id) in Objective-C

开发者 https://www.devze.com 2023-02-01 18:04 出处:网络
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)

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 bet开发者_如何转开发ter: id_vs_NSObject.


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 mean NSObject *?) is strongly typed--the compiler assumes it only responds to methods that Object is known to respond to.

0

精彩评论

暂无评论...
验证码 换一张
取 消