I am having problems understanding what/how this works. it seems odd assigning self to return from the init message
- (id)init
{
if((self=[super init])) {
//code here for setting up
}
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.
The
initmethod first assigns the implicitselflocal variable (selfis one of the two hidden arguments passed to methods) to the return value of the superclass’s designated initializer. The reason behind this is that initializers can return a different object than the one that received the message, for example, when it is not possible to initialize the receiver correctly or when an existing instance is returned to avoid the need to initialize a new one.After
selfis set, theifstatement ensures that instance variables are only initialized ifselfis notnil. Ifselfisnil, accessing the memory for the instance variables may be an error. Very few classes returnnilbut still it is a valid return value.This is described in Implementing an Initializer.