How can I make sure the object has completed initialization before using it?

Having written several Objective-C in one method, I call +alloc, then -init, to set up the object.

object = [[MyClass alloc] init];
[object useFor:whatever];

The next few lines of code use the newly created object. If the above -inittakes too much time, I am sure that the program will not β€œwait” before starting to use the new object, is it? If not, is there a quick way to guarantee completion -init?

Sometimes I see programmers writing something line by line

if(object = [[MyClass alloc] init]) {
    [object useFor:whatever];
}

Is that what I have to go for?

+3
source share
3 answers

, C Objective-C ( ) , , . , .

, , ( , concurrency), . C , , , , , .

: init , .

+3

init, , init. , init . , , .

- . if init. , , . init, self, , nil, ; , , , .

+9

, paralell

(, ;)

+4

Source: https://habr.com/ru/post/1754447/


All Articles