in Technology by (1.8m points)
What is instanceType in Objective-c?

1 Answer

0 votes
by (1.8m points)

@interface A :

NSObject — (instancetype)init;


@interface B : A


The init method from A is inherited to B. However, in both classes the method has a different return type. In A the return type is A and in B the return type is B.

There is no other way to declare the return type for initializers correctly. Note that most programming languages with classes don’t even have return types for constructors, therefore they completely avoid the issue.

This is the reason why Obj-C needs instancetype but of course it can be used outside initializers, too.

Related questions

0 votes
asked Nov 10, 2020 in Technology by JackTerrance (1.8m points)