什么是diff。 b / w @property(nonatomic,assign)和@property(nonatomic,retain) [英] what is diff. b/w @property (nonatomic,assign) and @property (nonatomic,retain)

查看:116
本文介绍了什么是diff。 b / w @property(nonatomic,assign)和@property(nonatomic,retain)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

我已经在许多类中看到使用委托,使用此关键字assign而不是retain。

i have seen in many classes that uses delegate which use this keyword assign instead of retain.

y我们使用assign代替retain。

y we have use assign instead retain.

可以使用assign给myclass ivar。

may i use assign to myclass ivar.

那么它的意义是什么?

推荐答案

您应该分配(=保留计数没有更改)委托, retain loops(不能想到一个更好的词)

You should assign (= no change in retain count) delegates because you want to avoid "retain loops" (can't think of a better word)

获取一个UITableView和一个UIViewController。

Take a UITableView and a UIViewController.

当您将UITableView添加到您的viewController时,您保留它。然后你分配一个委托和一个数据源(也是一个委托)到你的UITableView。通常这是你的viewController。

When you add the UITableView to your viewController you retain it. Then you assign a delegate and a datasource (which is a delegate too) to your UITableView. Usually this is your viewController.

如果tableview将保留数据源(你的viewController),会有一个保留循环

If the tableview would retain the datasource (your viewController) there would be a "retain loop"

viewontroller保留tableview。

tableview保留viewcontroller

viewontroller retains tableview.
tableview retains viewcontroller

viewcontrollers dealloc(释放tableview)永远不会释放你的viewcontroller。反之亦然。

The viewcontrollers dealloc (where you release the tableview) would never be called because tableview would never release your viewcontroller. And the other way around.

因为这两个都不会被释放。这就是为什么UITableView只分配数据源和委托。你应该在你的课程中做同样的事。

And because of this neither would get deallocated. That's why UITableView only assigns the datasource and the delegate. And you should do the same in your classes.

这篇关于什么是diff。 b / w @property(nonatomic,assign)和@property(nonatomic,retain)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆