Benj
Benj

Reputation: 245

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

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.

Upvotes: 1

Views: 2153

Answers (1)

Jonathan Grynspan
Jonathan Grynspan

Reputation: 43472

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.

Upvotes: 6

Related Questions