Property Declaration Attributes

题目不知道应该怎么翻译?硬是要翻译,只能是属性声明的特性。
 
对于这部分内容,《The Object C 2.0 Programming Language》的介绍是:
 
You can decorate a property with attributes by using the form @property(attribute[,attribute2,
...]). Like methods,properties are scoped to their enclosing interface declaration.For property
declarations that use a comma delimited list of variable names,the property attributes apply to all
of the named properties.If you use garbage collection,you can use the storage modifiers __weak and
__strong in a property’s declaration,but they are not a formal part of the list of attributes.
 
翻译一下:你可以通过以下格式去用attributes 修饰property :
 
@property(attribute[,attribute2,...])
就像方法一样,property的作用域限于它们自己封闭的接口声明中。尽管property的声明是使用了的attributes是被逗号分割的,但这些property attributes 的每一个都对property本身起作用。如果你使用GC,你可以在property的生命中使用weak和stronge这两个存储修饰符,但他们并不属于attributes的一部分。
 
接下来,介绍每一种attribute:
 
  • getter=getterName,setter=setterName:
   getter=和setter=修饰符用于指定property的get和set访问器(方法)。getter方法必须返回一个与property的类型匹配的值并且不需要带任何的参数访问该方法。setter方法必须带唯一的一个类型与property一致的参数并且返回值为void;
 
  • readOnly:
   声明property是readOnly的,默认为read/write。
也就是说在@implementation中,只有getter方法是需要的,或者,当使用@synthesized时,只会存在getter方法,而没有setter;
 
  • read/write:
   不解释。。。。
 
  • assign:
   指定setter方法只使用简单的赋值,这是默认的。
   如果你的应用使用GC时,如果你希望在遵守NSCopying协议的class中的property上使用assign时,你应该显式指明这个attribute,而不是希望系统默认使用它,否则的话,编译器会有警告。
 
  • retain:
   说明该对象的赋值时,对象需要保留(retain,也就是引用计数增加一次)(默认是assign)。
   这个attribute仅限于Object-C对象类型,不能对Core Foundation中的对象使用。
 
  • copy:
   说明赋值是赋给对象的拷贝。这个attribute仅使用于遵守NSCopying协议的对象类型上。
 
  • nonatomic
   指明访问器是非原子性的。默认是原子性的。
   默认的合成访问器(@synthesized修饰后产生的getter和setter方法)在多线程环境下是提供健壮的属性(property)访问的。这也就是说,getter和setter方法每一次都是“完整”的取值或赋值,不管有没有其他的线程在同时进行对该值的操作。
   如果没有使用nonatomic去修饰property,那么生成的getter会在每一次执行时都会retain一下对象,然后再autorelease,如果声明了的话,那么就直接get就ok了,这就避免了额外的开销。
 
#注意,如果不实用GC,在用assign、copy和retain时必须显示声明,不然会有警告。
 
 

转载于:https://www.cnblogs.com/chiefhsing/archive/2013/02/21/2920760.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值