加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift如何在属性属性中实现ARC?

发布时间:2020-12-14 02:29:30 所属栏目:百科 来源:网络整理
导读:Swift如何在属性属性中实现ARC?例如,如何让我的String变量在Swift中使用copy而不是strong? 如果需要Objective-C的复制行为,可以使用@NSCopying属性. 从Swift Book: Apply this attribute to a stored variable property of a class. This attribute causes
Swift如何在属性属性中实现ARC?例如,如何让我的String变量在Swift中使用copy而不是strong?
如果需要Objective-C的复制行为,可以使用@NSCopying属性.

从Swift Book:

Apply this attribute to a stored variable property of a class. This
attribute causes the property’s setter to be synthesized with a copy
of the property’s value—returned by the copyWithZone method—instead of
the value of the property itself. The type of the property must
conform to the NSCopying protocol.

The NSCopying attribute behaves in a way similar to the Objective-C
copy property attribute.

但是,在String属性的特定情况下,没有必要这样做.

字符串是Swift中的值类型.因此,当将现有String分配给新变量时,该变量实际上存储了String的副本,而不是对现有变量的引用.

Swift’s String type is a value type. If you create a new String value,
that String value is copied when it is passed to a function or method,
or when it is assigned to a constant or variable. In each case,a new
copy of the existing String value is created,and the new copy is
passed or assigned,not the original version.

因此,当您具有要使用复制行为设置的引用类型的属性时,将使用@NSCopying属性.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读