0 votes
in Technology by (321k points)
What is instanceType in Objective-c?

1 Answer

0 votes
by (321k points)

@interface A :

NSObject — (instancetype)init;

@end

@interface B : A

@end

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
1 answer
asked Nov 10, 2020 in Technology by JackTerrance (321k points)
0 votes
1 answer
asked Nov 10, 2020 in Technology by JackTerrance (321k points)
0 votes
1 answer
0 votes
1 answer
...