After touching the OC language for some time, we all know that an object of unknown type can be represented by an ID, so what is instancetype for?
The role of 1.instancetype
is to make the methods of the non-associative return type return the type of the class
2. Benefits of using Instancetype
A. The ability to determine the type of object can help the compiler to better locate code for us to write problems
B. The compiler is able to detect whether a method is implemented based on the returned data type, enabling developers to find errors during the compile phase.
The similarities and differences between 3.instancetype and ID
A. The same point
Can be used as the return type of a method
B. Different points①instancetype can return objects of the same type as the method's class, and the ID can only return objects of unknown type;
②instancetype can only be used as a return value, not as a parameter as an ID
3. Summary : Although using instancetype instead of ID as the return value does not have a technical benefit, this is because the compiler automatically converts the ID into instancetype. The beginner will assume that the value type returned by INIT is an ID, but the compiler returns INSTANCETYPE.
Finally, borrowing the nine words summed up by others to summarize all the above conclusions, using Instancetype has three advantages: explicit programmatic consistency.
For objective-c beginners, using instancetype and ID to define a simple constructor, exactly what is the difference?