Objective-C 中的弱和强属性 setter 属性

Objective-C 中弱属性和强属性 setter 属性的区别是什么?

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

影响和好处是什么?

我听说薄弱是不可用的 iOS4和我们需要使用分配。

弱点类似于分配吗?

90548 次浏览

You either have ARC on or off for a particular file. If its on you cannot use retain release autorelease etc... Instead you use strong weak for properties or __strong __weak for variables (defaults to __strong). Strong is the equivalent to retain, however ARC will manage the release for you.

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).

The 'toll free bridging' part (casting from NS to CF) is a little tricky. You still have to manually manage CFRelease() and CFRetain() for CF objects. When you convert them back to NS objects you have to tell the compiler about the retain count so it knows what you have done.

Its all here.

To call out the parts of the docs referenced by Robert that answer your last two questions explicitly:

// The following declaration is similar to "@property(assign) MyClass *myObject;"
// except that if the MyClass instance is deallocated,
// the property value is set to nil instead of remaining as a dangling pointer.
@property(weak) MyClass *myObject;

This is referred to as a zeroing weak reference. You can create weak references that are not zeroing weak references using __unsafe_unretained, but as the name implies, this is generally not recommended.

Also in the docs:

Weak references are not supported in Mac OS X v10.6 and iOS 4.

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).

let take an example to elaborate more(above answer are already great), may this example helps little more

let we have two class A and B

//A.h


#import <Foundation/Foundation.h>
#import "B.h"


@interface A : NSObject


@property (nonatomic, strong) B *objB;


@end


@implementation A
//


@end


//B.h


#import <Foundation/Foundation.h>
#import "A.h"




@interface B : NSObject


@property strong text(nonatomic, strong) A *objA;


@end


@implementation B
//


@end


and in main


#import "B.h"
#import "A.h"


{
A *obja =[[A alloc]init];
B *objb =[[B alloc]init];
A.objB=objb;
B.objA=obja;
}

the above code will generate a retain cycle because both are the strong type a-------->b--------->a

so to avoid it you have to use week property of one of it so that it weekly refer to the object and not increase it reference count.

Crystal clear use of WEAK property is as follows:

Any control whose properties we need to change(eg:text of a label) is declared weak and as below:


@property(nonatomic,weak) IBOutlet Type *name;
Eg: @property(nonatomic,weak) IBOutlet UILabel *myLabel;