转载:stackoverflow上对iOS中strong and weak的形象描述

http://stackoverflow.com/questions/9262535/explanation-of-strong-and-weak-storage-in-ios5

The difference is that an object will be deallocated as soon as there are no strong pointers to it. Even if weak pointers point to it, once the last strong pointer is gone, the object will be deallocated, and all remaining weak pointers will be zeroed out.

Perhaps an example is in order.

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.

时间: 2024-10-11 09:03:27

转载:stackoverflow上对iOS中strong and weak的形象描述的相关文章

stackoverflow上关于iOS的票数最多(最常见)的15个问题

搞编程做项目的,没碰到bug.遇到问题,基本不可能.stackoverflow就是一个大型的开放的FAQ平台,你是问题制造者,也是答案提供者.本文列出至今stackoverflow上关于iOS的票数最高(最常见)的15个问题,仅为了大家能够更方便.直接.快速的找到自己想要的答案,也许其中某个(些)问题就是你已经碰到或者即将碰到的. (这里的答案都是简单描述,有需要的话可以进入问题原页面查看详细答案) 一.怎么把UILabel的内容竖直方向靠上排列 我们都知道NSTextAlignment有五个值

ios中strong, weak, assign, copy

copy 和 strong(retain) 区别 1. http://blog.csdn.net/itianyi/article/details/9018567 大部分的时候NSString的属性都是copy,那copy与strong的情况下到底有什么区别呢? 比如: @property (retain,nonatomic) NSString *rStr; @property (copy, nonatomic)   NSString *cStr; - (void)test: { NSMutabl

IOS开发 strong,weak,retain,assign,copy nomatic 等的区别与作用

strong,weak,retain,assign,copy nomatic 等的区别 copy与retain:1.copy其实是建立了一个相同的对象,而retain不是:2.copy是内容拷贝,retain是指针拷贝:  3.copy是内容的拷贝 ,对于像NSString,的确是这样,但是如果copy的是一个NSArray呢?这时只是copy了指向array中相对应元素的指针.这便是所谓的"浅复制".4.copy的情况:NSString *newPt = [pt copy];此时会在

IOS5中strong和weak的用法

iOS 5 中对属性的设置新增了strong 和weak关键字来修饰属性(iOS 5 之前不支持ARC) strong 用来修饰强引用的属性: @property (strong) SomeClass * aObject; 对应原来的 @property (retain) SomeClass * aObject; 和 @property (copy) SomeClass * aObject; weak 用来修饰弱引用的属性:@property (weak) SomeClass * aObject

iOS中ARC的weak,strong,copy,assign使用场景

copy : NSString\NSMutableString\block weak : 代理\UI控件 strong : 其他OC对象 assign : 基本数据类型(int\float)\枚举\结构体

iOS strong 和weak的形象理解(真的很精典!)

转:http://stackoverflow.com/questions/9262535/explanation-of-strong-and-weak-storage-in-ios5 觉得讲的很容易理解 The difference is that an object will be deallocated as soon as there are no strong pointers to it. Even if weak pointers point to it, once the last

ARC中strong、weak、unsafe_unretained的区别

第一.strong关键字与retain关似,用了它,引用计数自动+1 如果person定义如下: @interface Person : NSObject @property(nonatomic,strong)Book *book1; @end (void)viewDidLoad { [super viewDidLoad]; @autoreleasepool { p1=[[Person alloc] init]; Book *book=[[Book alloc] init]; p1.book1=

iOS中的armv6、armv7、armv7s

armv6.armv7.armv7s是arm CPU的指令集,原则上是向下兼容的,如:iPhone4sCPU支持armv7,但它会兼容armv6,只是使用armv6指令可能无法充分发挥它的特性.iphone5CPU支持armv7s,它也会兼容armv7. 如果引用到第三方的库,以前在iphone4s下编译没有问题,但是换成iphone5之后,提示: Undefined symbols for architecture armv7s:  "_OBJC_CLASS_$_AMapView",

iOS中的copy 转载

小结iOS中的copy http://www.jianshu.com/p/5254f1277dba  转载自: 介绍copy和mutableCopy 介绍深拷贝与浅拷贝 block为什么要用copy copy相对于直接赋值的好处 总结 预备知识 : 内存的栈区 : 由编译器自动分配释放, 存放函数的参数值, 局部变量的值等. 其操作方式类似于数据结构中的栈. 内存的堆区 : 一般由程序员分配释放, 若程序员不释放, 程序结束时可能由OS回收. 注意它与数据结构中的堆是两回事, 分配方式倒是类似于