Matthijn
Matthijn

Reputation: 3234

Method signature return value "Class of type or subclass class"

When you have a signature like this:

- (UIView *)fooView;

You can return any subclass of UIView * (e.g UIScrollView)

And when you have:

- (Class)anyClass;

You can return any class (not an instance, the class itself) but is there a way to only allow classes of a certain class or subclass? E.g in psuedo code:

- ([UIView class])bazClass;

So here it should only be able to return a class UIView of any of its subclasses.

Upvotes: 1

Views: 644

Answers (2)

LombaX
LombaX

Reputation: 17364

As specified by other users, you can't.
If your goal is to instruct other programmers about what to return from a method in your code (overriden or delegate method), you can:

  • write in the comment (of course...)
  • create a typedef like this

.

typedef Class ClassOfKindUIView;

-(ClassOfKindUIView)class
{
    return [super class];
}

This won't block anything, but it can be a "talking method", an escamotage, to make the programmers stop and think "what is this??", then cmd-click and read the docs :-)

Upvotes: 2

Gabriele Petronella
Gabriele Petronella

Reputation: 108151

What you're looking for is a sort of type bound on the return type.

Unfortunately this is not possible in Objective-C, so I'm afraid you're out of luck.

Upvotes: 0

Related Questions