Comparison of Two Methods for creating objects in iOS development: ios objects
Comparison of Two Methods for creating objects in OC Syntax: 1. Using new to create objects; 2. Using alloc and Init to create objects.
Using new to create an object, the system will directly open up the memory and call the init method to initialize the object.
By using the alloc and init methods, the system first allocates memory to the variables, and then calls the Init Method for initialization. The New method is a summary of this method.
However:
1. In actual development, new is rarely used. Generally, when an object is created, [[className alloc] init] is used.
2. the difference is that when alloc allocates memory, zone is used to allocate memory to objects and the associated objects are allocated to an adjacent memory area, in this way, a small amount of memory is consumed during the call, improving the processing speed of the program.
3. new is not recommended. If new is used, the initialization method is fixed and only init can be used. Other initWithXX methods cannot be called.
4. Only the default init method can be used to complete initialization using the new method. Other customized initialization methods can be used using alloc.
5. the alloc init method is used to create objects. I can also override the init method of objects, but it is not allowed if new is used to create objects. Therefore, alloc is recommended, init to initialize a variable.