为什么 iOS 开发中,控件一般为 weak 而不是weakify strongify

trackbacks-0
strong关键字与retain关似,用了它,引用计数自动+1,用实例更能说明一切
@property&(nonatomic,&strong)&NSString&*string1; &&
@property&(nonatomic,&strong)&NSString&*string2; &
有这样两个属性,
&@synthesize&string1; &&
@synthesize&string2; &
猜一下下面代码将输出什么结果?
self.string1&=&@"String&1"; &&
self.string2&=&self.string1; &&
self.string1&=& &
NSLog(@"String&2&=&%@",&self.string2); &
结果是:String 2 = String 1由于string2是strong定义的属性,所以引用计数+1,使得它们所指向的值都是@"String 1", 如果你对retain熟悉的话,这理解并不难。
接着我们来看weak关键字:
如果这样声明两个属性:
@property&(nonatomic,&strong)&NSString&*string1; &&
@property&(nonatomic,&weak)&NSString&*string2; &
@synthesize&string1; &&
@synthesize&string2; &
再来猜一下,下面输出是什么?
&self.string1&=&@"String&1"; &&
self.string2&=&self.string1; &&
self.string1&=& &
NSLog(@"String&2&=&%@",&self.string2); &
结果是:String 2 = null
分析一下,由于self.string1与self.string2指向同一地址,且string2没有retain内存地址,而self.string1=nil释放了内存,所以string1为nil。声明为weak的指针,指针指向的地址一旦被释放,这些指针都将被赋值为nil。这样的好处能有效的防止野指针。在c/c++开发过程中,为何大牛都说指针的空间释放了后,都要将指针赋为NULL. 在这儿用weak关键字帮我们做了这一步。
阅读(...) 评论()为什么 iOS 开发中,控件一般为 weak 而不是 strong_百度知道
为什么 iOS 开发中,控件一般为 weak 而不是 strong
提问者采纳
weak只是一些数字类型看情况。alloc都是strong。我一般手动写代码
其他类似问题
为您推荐:
ios的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁2751人阅读
Ios(265)
今天用Xcode5的时候,发现默认的IBoutlet的属性设置为weak——因为Xcode5建立的工程都是ARC的了。但是当时还有点不明白,因为项目的原因,一直没有正式使用过ARC。于是,为了搞清楚为什么,google了一下,有很多答案。试着从Apple文档寻找线索,在找到了说明:
From a practical perspective, in iOS and OS X outlets should be defined as&. Outlets should generally be&weak, except for those from File’s Owner
to top-level objects in a nib file (or, in iOS, a storyboard scene) which should be&strong. Outlets that you create should therefore typically be&weak, because:
Outlets that you create to subviews of a view controller’s view or a window controller’s window, for example, are arbitrary references between objects that do not imply ownership.The strong outlets are frequently specified by framework classes (for example,&UIViewController’s&view&outlet, or&NSWindowController’swindow&outlet).
简单的说,如果IBOutlet对象是nib/sb scene的拥有者(File’s owner)所持有的对象,那么很显然拥有者必须“拥有”对象的指针,因此属性应设置为strong。而其他的IBOutlet对象的属性需要设置为weak,因为拥有者并不需要“拥有”他们的指针。举例来说,UIViewController的view属性是strong,因为controller要直接拥有view。而添加到view上的subviews,作为IBOutlet只需要设置为weak就可以了,因为他们不是controller直接拥有的。直接拥有subviews的是controller的view,ARC会帮助管理内存。
紧接着,又提到:
Outlets should be changed to&strong&when the outlet should be considered to own the referenced object:
As indicated previously, this is often the case with File’s Owner—top level objects in a nib file are frequently considered to be owned by the File’s Owner.You may in some situations need an object from a nib file to exist outside of its original container. For example, you might have an outlet for a view that can be temporarily removed from its initial view hierarchy and
must therefore be maintained independently.
第一种情形前面已经解释过了,对于第二种,通俗点将,就是controller需要直接控制某一个subview并且将subview添加到其他的view tree上去。
单纯从ARC的角度思考,用weak也是很显然的:因为subview添加到view上时,view会“拥有”subview。当然,给IBOutlet属性设置为strong也没有错,“纠结谁对谁错“的问题可能需要上升到模式或者编码习惯的问题,已经超出本文的范围。
最后附上stakoverflow上的答案:
/questions/7678469/should-iboutlets-be-strong-or-weak-under-arc/9141
除了选中的答案之外,其他人的回答也非常有意思,大家可以看看。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:654987次
积分:6520
积分:6520
排名:第1967名
原创:11篇
转载:285篇
评论:94条
(1)(3)(1)(4)(1)(5)(3)(4)(9)(40)(23)(52)(22)(11)(15)(3)(22)(35)(2)(2)(22)(2)(1)(1)(7)(3)(6)(6)(3)(5)(18)(23)(4)(1)}

我要回帖

更多关于 weak和strong的用法 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信