Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between strong and weak in Objective-C

Tags:

objective-c

I'm new to Obj-C, so my first question is:

What are the differences between strong and weak in @property declarations of pointers to objects?

Also, what does nonatomic mean?

like image 992
Mark Pegasov Avatar asked Jun 13 '12 11:06

Mark Pegasov


People also ask

What is the difference between strong and weak references?

The key difference between a strong and a weak or unowned reference is that a strong reference prevents the class instance it points to from being deallocated. That is very important to understand and remember. ARC keeps track of the number of strong references to a class instance.

What is weak reference in Objective C?

Pointers that are not retained are often referred to as “weak” in Objective-C documentation that predates the garbage collector. These are references that are allowed to persist beyond the lifetime of the object. Unfortunately, there is no automatic way of telling whether they are still valid.

What is strong property in Objective C?

strong (default) Strong just means you have a reference to an object and you will keep that object alive. As long as you hold that reference to the object in that property, that object will not be deallocated and released back into memory.

What is the difference between strong and weak references in C#?

A weak reference is a way to have some pointer to an object that does have any reference (strong reference). In . NET, any normal reference to another object is a strong reference . That is, when you declare a variable of a type that is not a primitive/value type, you are declaring a strong reference.


1 Answers

It may be helpful to think about strong and weak references in terms of balloons.

A balloon will not fly away as long as at least one person is holding on to a string attached to it. The number of people holding strings is the retain count. When no one is holding on to a string, the ballon will fly away (dealloc). Many people can have strings to that same balloon. You can get/set properties and call methods on the referenced object with both strong and weak references.

A strong reference is like holding on to a string to that balloon. As long as you are holding on to a string attached to the balloon, it will not fly away.

A weak reference is like looking at the balloon. You can see it, access it's properties, call it's methods, but you have no string to that balloon. If everyone holding onto the string lets go, the balloon flies away, and you cannot access it anymore.

like image 95
MJN Avatar answered Oct 17 '22 02:10

MJN