3

I have few questions.

1)where assign property will take memory as we dont need to release for reducing reference count?

2)what is the difference between auto zero reference and non-auto zero reference?how does it work? how will take memory?

Muthu Muthu
  • 139
  • 2
  • 9

2 Answers2

27

weak applies to objects (they have reference counts and all the stuff), but weak references don't increase refcount. But once the object is deallocated (from anywhere in the code), any weak reference to that object is set to nil. This is extremely useful, because if you use only strong and weak references, you can't end up with an invalid pointer (pointer to an already deallocated object).

assign does absolutely nothing with the reference, it is usually used for ints, floats and other non-object types. You can of course assign an object reference to such a variable, but if the object is deallocated, you will still have a pointer to it's memory (which is garbage now, and will hurt you when you use it).

Your concerns about "memory use" are weird - references don't take memory, object do. But you can't deallocate an object if you are going to use it. The simple rule for beginners is: for objects, use strong references whenever you can. When you have a reason not to use strong reference, use weak (usually for delegates and datasources). For primitive types (int, float, CGRect, ...) use assign, because they are not objects.

kuba
  • 7,026
  • 1
  • 34
  • 40
  • 1
    One correction to your description about 'weak'. Weak references don't get set to nil until the object is deallocated, not just released. – rmaddy Oct 12 '12 at 15:04
  • 1
    Also note that `assign` isn't allowed for object references under ARC. If you want that same behavior, you have to use `__unsafe_unretained`, which does the same thing, but intentionally sounds a lot scarier. – Ben Zotto Oct 15 '12 at 17:15
  • Although the answer has clear explanation about weak and strong reference but still I'm unable to understand why we use assign ? – Sukhpreet Jan 19 '17 at 06:07
8

assign is like weak but there's no zeroing of the pointer when it leaves the heap. So, it's not as safe as weak.

Snowcrash
  • 66,400
  • 60
  • 203
  • 323