Kevis
Kevis

Reputation: 43

id class in objective-c , why is it suitable for any classes?

I have a question about the class id in Objective-C. I don't know why it is suitable for any classes, like:

id objc = [[UIButton alloc] init]; 

I know that id class is a pointer to a struct called objc_object, but the right of the code return a memory address of an instance, whose size is different from the struct objc_object. So why it is ok to do this?

typedef struct objc_object *id;

Upvotes: 0

Views: 106

Answers (1)

Mojtaba Hosseini
Mojtaba Hosseini

Reputation: 119194

Memory addresses are same size. Even if you use UILabel * objc = [[UIButton alloc] init]; It works! The benefit of using KnownClassType is that compiler (and IDE) casts it and shows it's iterface to you even before running the app. But there is no guarantee for the type at runtime. id is just AnyObject type that you should take care of type casting for it yourself.

Upvotes: 1

Related Questions