First, what is Instancetype
Instancetype is clang 3.5, a keyword provided by clang that represents an unknown type of objective-c object returned by a method. We all know that an object of an unknown type can be represented by an ID keyword, so why is there another instancetype?
Ii. Association return type (related result types)
According to the naming rules of cocoa, the following rules are met:
1. In the class method, start with alloc or new
2. In the instance method, start with autorelease,init,retain or self
Returns an object of the class type of a method, known as a method that associates a return type. In other words, the return result of these methods is the type of the class in which the method is located, which is a bit around the mouth, see the following example:
[OBJC]View Plaincopy
- @interface NSObject
- + (ID) alloc;
- -(ID) init;
- @end
- @interface Nsarray:nsobject
- @end
When we initialize Nsarray using the following method:
[OBJC]View Plaincopy
- Nsarray *array = [[Nsarray alloc] init];
According to the Cocoa naming convention, the type of the statement [Nsarray alloc] is nsarray* because the return type of Alloc belongs to the associated return type. Similarly, the return result of[[Nsarray alloc]init] is also nsarray*.
Third, Instancetype role 1, function
If a method is not associated with the return type, as follows:
[OBJC]View Plaincopy
- @interface Nsarray
- + (ID) constructanarray;
- @end
When we initialize Nsarray using the following method:
[OBJC]View Plaincopy
- [Nsarray Constructanarray];
According to the cocoa Method naming specification, the resulting return type is the same as the return type of the method declaration, which is the ID.
However, if you use Instancetype as the return type, as follows:
[OBJC]View Plaincopy
- @interface Nsarray
- + (instancetype) Constructanarray;
- @end
When initializing Nsarray in the same way:
[OBJC]View Plaincopy
- [Nsarray Constructanarray];
The resulting return type and method are the same type as the class, which is nsarray*!
To summarize, the Instancetype function is to make the methods of the non-associative return type return the type of the class!
2. Benefits
Being able to determine the type of an object can help the compiler better position code for us, such as:
[OBJC]View Plaincopy
- [[[Nsarray alloc] init] Mediaplaybackallowsairplay]; //"No visible @interface for ' Nsarray ' declares the selector ' Mediaplaybackallowsairplay '"
- [[Nsarray array] Mediaplaybackallowsairplay]; //(No error)
The first line of code in the example above, as the result of [[Nsarray alloc]init] is nsarray*, This allows the compiler to detect whether the Nsarray implements the Mediaplaybackallowsairplay method based on the returned data type. Helps developers find errors during the compile phase.
The second line of code, because the array does not belong to the associated return type method,[Nsarray array] Returns the ID type, and the compiler does not know whether an object of the ID type implements the Mediaplaybackallowsairplay method, will not be able to find fault for developers in time.
Iv. similarities and differences between Instancetype and ID 1, the same point
Can be used as the return type of a method
2, 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 like an ID, as in the following notation:
[OBJC]View Plaincopy//err,expected a type
- -(void) SetValue: (instancetype) value
- {
- //do something
- }
Is wrong and should be written as:
[OBJC]View Plaincopy
- -(void) SetValue: (ID) value
- {
- //do something
- }
Instancetype and ID