为什么init方法以"return self"结尾?而不是"返回0;"?

Nov*_*ovo -4 objective-c ios

我为什么要"回归自我"; 在课程结束而不是"返回0;"?这两个陈述有什么区别?

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    //Call the init method implemented by the superclass
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
       //Create two arrays and make the pointers point to them
       questions = [[NSMutableArray alloc] init];
       answers = [[NSMutableArray alloc] init];

       //Add questions and answers to the arrays
       [questions addObject:@"What is 7 + 7?"];
       [answers addObject:@"14"];

       [questions addObject:@"What is the capital of Vermont?"];
       [answers addObject:@"Montpelier"];

       [questions addObject:@"From what is cognac made?"];
       [answers addObject:@"Grapes"];

   }

   // Return the address of the new object
   return self;
}

@end
Run Code Online (Sandbox Code Playgroud)

小智 6

因为return 0;会返回一个NULL指针.不是人们期望从已成功执行的初始化程序(正是因为它应该返回它已初始化的对象 - 否则你将丢失该对象).初始化器不是main().