2

What is the difference between strong vs copy in objective-c? Which one should i be using?

I know copy prevents the value of the instance variable from changing if set with a mutable string that is later changed itself. Anything else?

Kermit the Frog
  • 3,949
  • 7
  • 31
  • 39
  • 1
    "Which one should I be using?" It depends upon your needs. Sometimes you need to make sure that your class has its own copy or that it cannot be mutated, in which `copy` is correct. Sometimes you don't care and really want a `strong` reference to some other object. Both `strong` and `copy` (as well as `weak`) have situations where they are, respectively, appropriate. Ask a question about a particular scenario, and we can help you (if it's not already obvious after reading the post to which Josh referred you). – Rob Nov 11 '13 at 21:34

1 Answers1

1

strong increases retain counter of an object by 1.

copy creates an object's copy with retain counter 1.

If you use ARC you can't access to retain counter, but the approach is the same as for MRC.

Sviatoslav Yakymiv
  • 7,887
  • 2
  • 23
  • 43
  • This is incorrect for copy. – zaph Nov 11 '13 at 21:38
  • 3
    `copy` of an immutable object such as NSString just increases the retain count. `copy` of a mutable object makes an immutable copy with a retain count of 1. This is for properties of immutable objects. – zaph Nov 11 '13 at 21:41
  • That is why `copy` is recommended for `NSString`, `NSNumber`, it only makes a copy of mutable to immutable. – zaph Nov 11 '13 at 21:45