1

I have read several questions but I am still confused. I read this: Objective-C ARC: strong vs retain and weak vs assign and some other questions

When we say: someObject.color = customColor

Are we increasing the reference count of customColor by retaining it or we are creating a new object by copying it? Or none?

Community
  • 1
  • 1
mfaani
  • 33,269
  • 19
  • 164
  • 293

3 Answers3

3

That statement is assigning to a property, i.e. calling a setter method on someObject's class. So, that would depend on how the method is implemented. If that is an autogenerated setter method based on the property, then: if it is a copy property, the -copy method will be called on customColor; if strong then it will be retained (i.e. increase the reference count), or if weak or assign then it will not increase the reference count.

Be aware that on some immutable classes, the -copy method is implemented to just return self, so that will be just like a retain -- it is not always a different object reference.

Carl Lindberg
  • 2,902
  • 18
  • 22
  • Thanks, so if it is a copy property, it won't increase the reference count of customColor, but it would increase the reference count of a newly copied object? – mfaani Apr 08 '16 at 20:23
  • 1
    You can easily check that yourself by looking at the object's address. With immutable objects that is really an implementation detail that you shouldn't care about - because it may work either way, or change now or later. Your contract just says "gimme something that behaves like a copy". – Eiko Apr 08 '16 at 21:06
  • 1
    @asma22 -- that depends on how the -copy method is implemented on the receiver. For an immutable object like NSNumber, it will actually just increase the retain count. But for something like NSMutableArray, then yes, a new (immutable) NSArray instance will be created and stored. I believe UIColor is one of those immutable classes -- so in that particular case, it will just increase the retain count. Conceptually it is another object but immutable classes can optimize that operation, since there is no way to modify the instance. – Carl Lindberg Apr 10 '16 at 02:46
1

As the official documentation states:

There are three kinds of retainable object pointer types:

  • block pointers (formed by applying the caret (^) declarator sigil to a function type)
  • Objective-C object pointers (id, Class, NSFoo*, etc.)
  • typedefs marked with attribute((NSObject))

When we are dealing with the Objective-C object pointers (patterns of bits indicating the location of a pointed-to object) and the code is like this:

  UIColor *color1 = [UIColor whiteColor];

  // Increase reference count of color1 by 1, without copying it.
  UIColor *color2 = color1;

during an assignment the objects are not being copied, but their references are (with increasing reference count).

Regarding your case, as was stated in the other comment, it depends on the property attributes: the copy attribute creates a brand new instance of the object and increments its reference count, leaving the original object unaffected.

By the way, you can verify reference count of your objects using the Allocations Instrument. Check out, for example, this nice answer to know how you can manage it.

Stan Mots
  • 1,193
  • 2
  • 15
  • 16
0

I found this image from Apple's own documentation, simply if the color property is a strong pointer then it will increase the retain count, and if it's a copy pointer then it will create a new object with the retain count of 1

memory_management

mfaani
  • 33,269
  • 19
  • 164
  • 293