I already know when no strong references are there for the object. But can anybody give an example.
-
Read the docs: https://developer.apple.com/library/ios/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html – Sandy Chapman Jul 28 '15 at 11:25
-
"How do I prevent it from being deallocated?" You simply have to keep a strong reference to it somewhere. This is the purpose of `weak`, to _not_ hang on to it when there are no more strong references elsewhere. The question doesn't make sense. Perhaps give us practical example of when you're imagining this scenario arising. – Rob Jul 28 '15 at 11:25
1 Answers
Object that have only weak references will get released at the nearest occasion.
It has been said before in this topic:
Differences between strong and weak in Objective-C
A strong reference (which you will use in most cases) means that you want to "own" the object you are referencing with this property/variable. The compiler will take care that any object that you assign to this property will not be destroyed as long as you (or any other object) points to it with a strong reference. Only once you set the property to nil will the object get destroyed (unless one or more other objects also hold a strong reference to it).
In contrast, with a weak reference you signify that you don't want to have control over the object's lifetime. The object you are referencing weakly only lives on because at least one other object holds a strong reference to it. Once that is no longer the case, the object gets destroyed and your weak property will automatically get set to nil. The most frequent use cases of weak references in iOS are:
delegate properties, which are often referenced weakly to avoid retain cycles, and
subviews/controls of a view controller's main view because those views are already strongly held by the main view.
atomic vs. nonatomic refers to the thread safety of the getter and setter methods that the compiler synthesizes for the property. atomic (the default) tells the compiler to make the accessor methods thread-safe (by adding a lock before an ivar is accessed) and nonatomic does the opposite. The advantage of nonatomic is slightly higher performance. On iOS, Apple uses nonatomic for almost all their properties so the general advice is for you to do the same.
You should just make a strong reference, maybe like this:
@property (strong) NSNumber myNumber;
-
-
Suppose a NSMutableString property is given a weak attribute now i had done self.myproperty = @"testString"; Until when self.myproperty will print the value"testString". And will it be different if i take an NSString Object. – AnishGupta Jul 28 '15 at 13:18
-
In this scenario, the moment the object that is considered as "self" in your example gets deallocated, the print will stop working, as the string is a direct property of the "self" object. The same goes for NSString. Maybe try to think again about your question, and write it more precisely? Because I am having a hard time understanding it, and it would make more people try to help you. Write exactly what you want to do. – Snacks Jul 29 '15 at 07:02
-
My question is that when will be a weak property gets dellocated. if from the moment it is created there are no strong references. – AnishGupta Jul 29 '15 at 09:27