We always alloc before init in Objective C, then while writing init method, Why we don’t alloc a super and then initiate it?
- (id) init {
if(self = [super init]){
//init iVars
}
return self;
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
When the child interface is allocated, its size includes the size of the parent. They aren’t separate objects, they’re combined. ‘super’ must be initialized so it can prepare its data members. Your object then initializes its members. For example, lets say you had a custom reference counting interface that needs to initialize ‘refCnt’ to 1. You then base your interface on that interface. That means your interface’s allocation size is the size of the reference counting interface + the size of your specific contributions. (e.g., ‘int refCnt’ in the reference counting interface and ‘int x’ in your interface means sizeof(your interface)==8~.) So, the allocation only needs to occur once. Then, you initialize ‘super’ (the reference counting interface, in this situation) so ‘refCnt’ will be 1. After that, you initialize your own data.