What is the difference between alloc, copy and new?

What is the actual difference between alloc, copy and new, as well as the purpose and non-atomic property.

+4
source share
1 answer
  • Alloc

alloc is a class selector (which, for example, is called similar)

[NSObject alloc] 

It returns an uninitialized pointer of type NSObject *.

  • Init

To complete the initialization, you must call the appropriate designated initializer in the instance itself:

 [[NSObject alloc] init] 

Will return a useful NSObject * pointer.

  • new

The new one basically does alloc โ†’ init, except that it is called directly at the class level:

 NSObject* aObj = [NSObject new] NSObject* aObj = [[NSObject alloc] init] 

Are similar.

  • non-atomic

A non-atomic property means that when a property is written (ex during a given call) , no lock will be added to the variable synthesized by this property (this means that you do not need to overload @synchronize time).

So, if your property is not changed by different threads at the same time, you can safely set it to non-atomic.

  • copy

The copy property means that when this property is changed ex:

 aObj.copyProperty = otherValue 

The copyProperty variable will send a copyWithZone: signal to the otherValue object.

In other words, if your copyProperty is compatible with the NSCopying protocol, it will ultimately have the same properties as otherValue, but will have its own address and store the counter and therefore be in a completely different part of the memory, since there was anotherValue.

Basically copyProperty will take up as much memory space as otherValue.

  • assignee

Assigning a property means when you do:

 aObj.prop = aProperty 

A variable synthesized using the prop property will be directly assigned to aProperty, meaning that they will pass the exact same address and keep count .

Extra memory space is not used when you use assignment.

Hope this helps you. For more information, please read the Apple Memory Management Documentation.

+5
source

All Articles