The CS193p course says that the if statement must be in the init method to check if it works [super init]:
[super init]
if (self = [super init]) { self.someProperty = parameter; } return self;
I don’t understand why this is done, as if [super init]returning nil, does the method itself also return nil, regardless of the result of the if statement?
EDIT: Question: why put self = [super init]inside an if statement. (Not: why self = [super init]at all)
self = [super init]
This makes sense, because in some cases [super init] may return nil, and in this case, if you try to access some ivar, you will fail.
Example:
-(id) init { self = [super init]; some_ivar = [[NSObject alloc] init]; //compiler treats this as self->some_ivar //if self == nil you'll get EXC_BAD_ACCESS return self; }
Apple Objective-c . ( " " )
Apple if
self = [super init]; if (self) { //init stuff... } return self;
LLVM 2.0, , . :
if ((self = [super init])) { //init stuff... } return self;
if(self = [super init]){.... init , alloc 'd.
if(self = [super init]){...
init
alloc
[super init], , . init , .
[super init] self Objective-C. , , , . , NSString
, self = [super init]
[[super alloc] init], [super init], , ,
if.
if
self = [super init]; if ( self ) { self.someProperty = parameter; } return self;
if self, self.someProperty = parameter
self
self.someProperty = parameter
self = [[super alloc] init]; creates a base class and the runtime suffocates when you try to call methods only for a subclass later.
self = [super init]; - normal vision in Objective-C when the base class needs to initialize the values of variables or create composite elements, otherwise they are created using the values Nil or (0).
Source: https://habr.com/ru/post/1794268/More articles:SQL Query to find all possible paths - sqlbest way to have separate stylesheets for different actions in Rails - ruby-on-railsIs there a way for aliases in WPF? - wpfWhat is the correct way to display complex dialogue in an Eclipse plugin? - javaNSMutableArray crashes when added after proper initialization - type-conversionRsync symlinks help - rsyncJavascript canvas side detection - javascriptЯ хочу добавить окно JLabel и Text динамически, нажав кнопку добавления - javareading 2 lines from IniFile - delphiКак предотвратить ошибки повторной записи при использовании классов Mock, реализующих интерфейс IteratorAggregate при тестировании с помощью PHPUnit? - phpAll Articles