Difference Between instancetype and id in Objective-C
Q: What is the difference between instancetype and id?
- Objective C
- Mid level question
Explore all the latest Objective C interview questions and answers
ExploreMost Recent & up-to date
100% Actual interview focused
Create Objective C interview for FREE!
Both `instancetype` and `id` are used to declare variables that can hold object references in Objective-C. However, there is a difference between the two:
- `instancetype` is a type that is used to declare a variable that should hold an instance of the class that is currently being defined. It is used as the return type of methods that create and return an instance of the class, such as `init` methods. The advantage of using `instancetype` is that it provides better type checking at compile-time, ensuring that the correct type of object is returned.
Example:
@interface MyClass: NSObject - (instancetype)init; @end
- `id` is a generic object type that can be used to declare variables that can hold any object reference. It can be used as the parameter or return type of methods that can take or return any type of object. The disadvantage of using `id` is that it does not provide any type checking at compile-time, so it is possible to call methods on objects that do not actually respond to those methods, leading to runtime errors.
Example:
id myObject = [[MyClass alloc] init];
In summary, `instancetype` is a type that is used to declare variables that hold instances of the class being defined, while `id` is a generic type that can hold any object reference. The main difference between the two is that `instancetype` provides better type checking at compile-time, while `id` does not provide any type checking at all.


