Vojto
Vojto

Reputation: 6949

'id' that points to some class or any subclass

so in Objective-C I can have a reference to object that implements certain protocol like this:

id<MyAwesomeProtocol> object;

I could have a reference to some class, like this:

MyAwesomeClass *object;

I was wondering, what should I do to have reference to MyAwesomeClass, or any sub-class.

I could of course just use id but I'd like to know if there is any better way.

Thanks!

Upvotes: 0

Views: 201

Answers (2)

diederikh
diederikh

Reputation: 25271

In general it is better to use NSObject* instead of id. All objective-C objects are decendants for NSObject so why ignore all the NSObject goodies?

If you are dealing with MyAwesomeClass or subclasses I would use MyAwesomeClass *object. If you are just using the protocol methods (in the context of the protocol) then I would use NSObject <MyAwesomeProtocol> *object.

Upvotes: 1

Vladimir
Vladimir

Reputation: 170829

To have reference to an object of your class or its subclasses you still can write

MyAwesomeClass *object;

Upvotes: 4

Related Questions