Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weak and strong property setter attributes in Objective-C

What is the difference between weak and strong property setter attributes in Objective-C?

@property(retain, [weak/strong]) __attribute__((NSObject)) CFDictionaryRef myDictionary; 

What is the impact and benefit?

I heard that weak is not available on iOS 4 and we need to use assign.

Is weak similar to assign?

like image 925
kkurni Avatar asked Oct 27 '11 06:10

kkurni


People also ask

What is strong and weak in Objective-C?

strong is the default. An object remains “alive” as long as there is a strong pointer to it. weak specifies a reference that does not keep the referenced object alive. A weak reference is set to nil when there are no strong references to the object.

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 strong attribute of property?

strong / retain : Declaring strong means that you want to “own” the object you are referencing. Any data 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.

What is weak reference in Objective-C?

A weak reference has no effect on the lifecycle of the object it refers to: when no strong references to the object remain, even if there are still weak references, the object gets deallocated, and any weak references to the object are set to nil .


1 Answers

Here is what I know about variable properties

  1. atomic //default
  2. nonatomic
  3. strong=retain //default
  4. weak
  5. retain
  6. assign //default
  7. unsafe_unretained
  8. copy
  9. readonly
  10. readwrite //default

so below is the detailed article link where you can find above mentioned all attributes, that will definitely help you. Many thanks to all the people who have given the best answers here!!

Variable property attributes or Modifiers in iOS

01.strong (iOS4 = retain ) - it says "keep this in the heap until I don't point to it anymore" - in other words " I'm the owner, you cannot dealloc this before aim fine with that same as retain" - You use strong only if you need to retain the object. - By default, all instance variables and local variables are strong pointers. - We generally use strong for UIViewControllers (UI item's parents) - strong is used with ARC and it basically helps you, by not having to worry about the retain count of an object. ARC automatically releases it for you when you are done with it. Using the keyword strong means that you own the object.

Example:

@property (strong, nonatomic) ViewController *viewController;  @synthesize viewController; 

02.weak (iOS4 = unsafe_unretained ) - it says "keep this as long as someone else points to it strongly" - the same thing as assign, no retain or release - A "weak" reference is a reference that you do not retain. - We generally use weak for IBOutlets (UIViewController's Childs).This works because the child object only needs to exist as long as the parent object does. - a weak reference is a reference that does not protect the referenced object from collection by a garbage collector. - Weak is essentially assign, a unretained property. Except the when the object is deallocated the weak pointer is automatically set to nil

Example :

@property (weak, nonatomic) IBOutlet UIButton *myButton;  @synthesize myButton; 

Explain:Thanks to BJ Homer

Imagine our object is a dog, and that the dog wants to run away (be deallocated). Strong pointers are like a leash on the dog. As long as you have the leash attached to the dog, the dog will not run away. If five people attach their leash to one dog, (five strong pointers to one object), then the dog will not run away until all five leashes are detached. Weak pointers, on the other hand, are like little kids pointing at the dog and saying "Look! A dog!" As long as the dog is still on the leash, the little kids can still see the dog, and they'll still point to it. As soon as all the leashes are detached, though, the dog runs away no matter how many little kids are pointing to it. As soon as the last strong pointer (leash) no longer points to an object, the object will be deallocated, and all weak pointers will be zeroed out. When we use weak? The only time you would want to use weak, is if you wanted to avoid retain cycles (e.g. the parent retains the child and the child retains the parent so neither is ever released).

like image 70
swiftBoy Avatar answered Sep 16 '22 12:09

swiftBoy