Object-c:Property Attributes && Variable life time Qualifiers

小弟不才,难以总结一篇好的文章, 故转自StackOverflow ~

转自:http://stackoverflow.com/questions/8927727/objective-c-arc-strong-vs-retain-and-weak-vs-assign

After reading so many articles Stackoverflow posts and demo applications to check variable property attributes, I decided to put all the attributes information together:

  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

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 give best answers here!!

Variable property attributes or Modifiers in iOS

1.strong (iOS4 = retain )

  • it says "keep this in the heap until I don't point to it anymore"
  • in other words " I'am 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;

2.weak -

  • 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;

Strong & Weak Explanation, 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).

3.retain = strong

  • it is retained, old value is released and it is assigned retain specifies the new value should be sent
  • retain on assignment and the old value sent -release
  • retain is the same as strong.
  • apple says if you write retain it will auto converted/work like strong only.
  • methods like "alloc" include an implicit "retain"

Example:

@property (nonatomic, retain) NSString *name;

@synthesize name;

4.assign

  • assign is the default and simply performs a variable assignment
  • assign is a property attribute that tells the compiler how to synthesize the property's setter implementation
  • I would use assign for C primitive properties and weak for weak references to Objective-C objects.

Example:

@property (nonatomic, assign) NSString *address;

@synthesize address;


手动翻译如下:

在读过很多篇文章和StackOverflow帖子还有应用demo去检验Variable property attributes,我决定将所有有关属性信息汇总在一起:

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

下面是你可以找到以上提到的所有属性的文章,这一定会帮到你,非常感谢这里的所有朋友!

http://rdcworld-iphone.blogspot.in/2012/12/variable-property-attributes-or.html   (被墙了,翻墙看)



1.strong: (iOS4 = retain)

·      这表明了“将它保存在堆中知道不再指向任何东西”

·      换句话说“我是持有者,你不能销毁它在指向好目标之前(这个语境应该是说在处理好之前aim fine),这与retain作用一样”

·      你使用strong属性仅仅因为你需要去retain对象

·      默认地,所有实例变量和局部变量都是strong型指针

·      我们通常地为UIViewControlls(UI视图元素的载体)使用strong属性

·      属性是用在ARC环境下的和这基本能帮助到你,使得你不用担心retain对象的计数。ARC环境自动帮你释放了当你用完了对象,使用关键字strong意味着你你强引用(强持有)这个对象。

例子:

@property (strong, nonatomic) ViewController *viewController;

@synthesize viewController;



2.weak

·      这表示“保持对象只要有别的对象强引用它(这里它,指的是内存块)”

·      与assign相同作用,没有retain和release

·      一个“weak”引用是一个没有经过retain的引用(这里说得太棒了) 

·      我们通常需要为IBOutlets(UIViewController‘s 的子项)使用weak关键字,它能有效因为子对象仅仅需要当父对象存在时才存在(这就不是weak的作用么)

·      Weak引用是一种不会去保护从垃圾回收机制的集合中的对象

·      Weak引用实质上就是assign(除了说一个应用在对象一个是基本类型数据),一种不经retain的属性,除了对象被主动销毁,weak指针会自动指向nil值

例子:

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

Strong&Weak 解释, 非常感谢 BJ Homer:

想象一下,我们对象是一只狗,而这只狗想离开torun(被释放)

Strong指针就像狗身上的狗拴,只要你有狗拴,这只狗就走不开了,如果有5个人都拴住了同一只狗(5个强引用指针指向一个对象),那么只要还存在一个人拉着狗拴这只狗都跑不了(即这个内存块不会被销毁只要有一个对象指向它,尼玛就是和retain一样的嘛)。

Weak指针,从另一个方面来说,就像一个小朋友指着一只狗说“你看,有一只狗!”只要这只狗仍然被拴着,这个小朋友仍然可以看到这只狗,即他们还是指向它(内存块),一旦所有的拴带被卸下,这只狗马上就能逃跑不管多少个小朋友指向它(形容得超好,小朋友 = weak 不足以指向它,所以被释放)。只要指向共存的一块内存地址的最后一个强引用指针不再指向它,对象就会被销毁,而weak指针就会自动指向nil/null(zero out)。

那么我们什么时候使用weak呢?

只有一种情况会使用weak指针,就是如果你想防止循环引用(例如父载体retains子载体:(这里的载体意味着很多哦)而子载体retains父载体导致永远都不被释放)。



3.retain = strong

·      旧值将被释放并被分配新值,retain确定了应该被传送的新值

·      在分配新值上的retain和旧值上应该发送release

·      retain和strong作用一样

·      苹果文档说了如果你写retain这将自动转化成strong型/与strong型作用效果一样

·      如“alloc”(带有“alloc”)的方法包换一个隐含的“retain”,这就是人们常说的手动alloc,就是retain嘛,然后存堆里了

例子:

@property (nonatomic, retain) NSString *name;  
@synthesize name;



4.assign

·      assign是缺省的,只是简单地执行值分配

·      assign是一种属性特性告诉编译器如何去合成属性的setter实现

·      我会为C的基本数据类型使用assign关键字而为Object-C对象使用weak引用(为什么放在一起比较呢,因为呢,两个都不引起retain嘛)

例子:

@property (nonatomic, assign) NSString *address; 
@synthesize address;




待续...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值