ios – 为什么在使用ARC的快速枚举循环中需要__strong

ios – 为什么在使用ARC的快速枚举循环中需要__strong,第1张

概述当我做下面的事情时,我得到一个错误说 for (UIView* att in bottomAttachments) { if (i <= [cells count]) { att = [[UIView alloc] extraStuff] }} 无法在ARC中修改快速枚举变量:声明__strong __strong做了什么以及为什么要添加它? If a varia 当我做下面的事情时,我得到一个错误说

for (UIVIEw* att in bottomAttachments) {    if (i <= [cells count]) {        att = [[UIVIEw alloc] extraStuff]    }}

无法在ARC中修改快速枚举变量:声明__strong

__strong做了什么以及为什么要添加它?

解决方法

If a variable is declared in the condition of an Objective-C fast enumeration loop,and the variable has no explicit ownership qualifIEr,then it is qualifIEd with const __strong and objects encountered during the enumeration are not actually retained.

Rationale
This is an optimization made possible because fast enumeration loops promise to keep the objects retained during enumeration,and the collection itself cannot be synchronously modifIEd. It can be overrIDden by explicitly qualifying the variable with __strong,which will make the variable mutable again and cause the loop to retain the objects it encounters.

source

正如Martin在评论中指出的那样,值得注意的是,即使使用__strong变量,通过重新分配它你也不会修改数组本身,但是你只需要将局部变量指向另一个对象.

在迭代数组时对数组进行变换通常是一个坏主意.只需在迭代时构建一个新数组,你就可以了.

总结

以上是内存溢出为你收集整理的ios – 为什么在使用ARC的快速枚举循环中需要__strong全部内容,希望文章能够帮你解决ios – 为什么在使用ARC的快速枚举循环中需要__strong所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1035067.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存