Differences between new and alloc/init for iOS development

Source: Internet
Author: User

Http://macresearch.org/difference-between-alloc-init-and-new:

1. In actual development, new is rarely used. Generally, all we see when creating objects is [[className alloc] init].

But it does not mean that you will not be exposed to new. In some code, you will still see [className new],

You may also be asked this question during the interview.

2. What is the difference between them?

Let's look at the source code:

 
 
  1. + New
  2. {
  3. Id newObject = (* _ alloc) (Class) self, 0 );
  4. Class metaClass = self-> isa;
  5. If (class_getVersion (metaClass)> 1)
  6. Return [newObject init];
  7. Else
  8. Return newObject;
  9. }
  10.  
  11. // While alloc/init is like this:
  12. + Alloc
  13. {
  14. Return (* _ zoneAlloc) (Class) self, 0, malloc_default_zone ());
  15. }
  16. -Init
  17. {
  18. Return self;
  19. }

Through the source code, we found that [className new] is basically equivalent to [[className alloc] init];

The difference is that zone is used when alloc allocates memory.

What is this zone?

When allocating memory to objects, it allocates the associated objects to an adjacent memory area, so as to consume a small amount of money during calls and increase the processing speed of the program;

3. Why is new not recommended?

I don't know if you have found it: if you use new, the initialization method is fixed and you can only call init.

What if you want to call initXXX? No! It is said that the initial design fully draws on the Smalltalk syntax.

Legend has it that allocFromZone was available at that time: This method,

However, This method requires passing the parameter id myCompanion = [[TheClass allocFromZone: [self zone] init];

This method is as follows:

 
 
  1. + AllocFromZone :( void *) z
  2. {
  3. Return (* _ zoneAlloc) (Class) self, 0, z );
  4. }
  5.  
  6. // Simplified to the following:
  7. + Alloc
  8. {
  9. Return (* _ zoneAlloc) (Class) self, 0, malloc_default_zone ());
  10. }

However, there is a problem: This method only allocates memory to the object and does not initialize instance variables.

Does it return to the processing method like new: Call the init method implicitly inside the method?

Later I found that "display call is better than implicit call", so I separated the two methods.

In summary, new and alloc/init functions almost the same, allocating memory and completing initialization.

The difference is that the new method can only use the default init method to complete initialization,

You can use other custom initialization methods using alloc.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.