zoukankan      html  css  js  c++  java
  • 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时必须显示声明,不然会有警告。
     
     
  • 相关阅读:
    HDU 2047 阿牛的EOF牛肉串
    HDU 2015 偶数求和
    HDU 2029 算菜价
    HDU 2028 Lowest Common Multiple Plus
    动态函数库设计
    静态函数库设计
    Linux编程规范
    Linux应用程序地址布局
    Core Dump 程序故障分析
    W-D-S-UART编程
  • 原文地址:https://www.cnblogs.com/chiefhsing/p/2920760.html
Copyright © 2011-2022 走看看