differences between weak and assign property?

后端 未结 2 1025
不知归路
不知归路 2021-02-03 16:30

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 zer

相关标签:
2条回答
  • 2021-02-03 16:47

    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.

    0 讨论(0)
  • 2021-02-03 16:56

    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.

    0 讨论(0)
提交回复
热议问题