instancetype vs class name for singleton?

后端 未结 2 1530
醉梦人生
醉梦人生 2021-02-19 07:36

From what I understand, instancetype declares to the compiler that the return type of the method is the same as the class receiving the message.

Traditionally I\'ve alwa

2条回答
  •  醉梦人生
    2021-02-19 08:29

    Constructor methods traditionally have returned id, allowing subclasses to use them too.

    id, of course, means "any object at all". instancetype was introduced to give a little more type strictness when assigning the result of a constructor method.

    It's only useful in case of subclassing. If that method will never be overridden, it's better to be as explicit as possible and use the actual class name.

提交回复
热议问题