Why should I not separate alloc and init?

后端 未结 4 1169
醉梦人生
醉梦人生 2021-01-22 21:05

The normal way to initialise and allocate in Objective-C is

NSObject *someObject = [[NSObject alloc] init];

Why is the following not practised

4条回答
  •  终归单人心
    2021-01-22 21:51

    Because it is less simple and more error-prone.

    An allocated but not initialised object is useless, so it make sense to put allocation and initialisation in one line. If they are separated, there is more possibility for errors and bugs if the two lines are not directly after each other (perhaps after refactoring), which may lead to errors while trying to use an uninitialised object.

    There simply isn't a single good reason to alloc and init in separate lines, and many reasons against it.

提交回复
热议问题