@property常用属性的解释
2012-05-04 14:05
211 查看
闲来无事把OC中的一些基础知识整理了一下。
variable names, the property attributes apply to all of the named properties.
If you use the
“Property Implementation Directives”), the code it generates
matches the specification given by the keywords. If you implement the accessor methods yourself, you should ensure that it matches the specification (for example, if you specify
propertyName and
Specifies the name of the get accessor for the property. The getter must return a type matching the property’s type and take no parameters.
Specifies the name of the set accessor for the property. The setter method must take a single parameter of a type matching the property’s type and must return
If you specify that a property is
Typically you should specify accessor method names that are key-value coding compliant (see
Key-Value Coding Programming Guide)—a common reason for using the
Indicates that the property should be treated as read/write. This attribute is the default.
Both a getter and setter method are required in the
Indicates that the property is read-only.
If you specify
Specifies that there is a strong (owning) relationship to the destination object.
Specifies that there is a weak (non-owning) relationship to the destination object.
If the destination object is deallocated, the property value is automatically set to
(Weak properties are not supported on OS X v10.6 and iOS 4; use
Specifies that a copy of the object should be used for assignment.
The previous value is sent a
The copy is made by invoking the
attribute is valid only for object types, which must implement the
protocol.
Specifies that the setter uses simple assignment. This attribute is the default.
You use this attribute for scalar types such as
Specifies that
the object upon assignment.
The previous value is sent a
In OS X v10.6 and later, you can use the
Specifies that accessors are nonatomic. By default, accessors are atomic.
Properties are atomic by default so that synthesized accessors provide robust access to properties in a multithreaded environment—that is, the value returned from the getter or set via the setter is always fully retrieved or set regardless of what other
threads are executing concurrently.
If you specify
roperty Declaration Attributes
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 the
@synthesizedirective to tell the compiler to create the accessor methods (see
“Property Implementation Directives”), the code it generates
matches the specification given by the keywords. If you implement the accessor methods yourself, you should ensure that it matches the specification (for example, if you specify
copyyou must make sure that you do copy the input value in the setter method).
Accessor Method Names
The default names for the getter and setter methods associated with a property arepropertyName and
setPropertyName
:respectively—for example, given a property “foo”, the accessors would be
fooand
setFoo:. The following attributes allow you to specify custom names instead. They are both optional and can appear with any other attribute (except for
readonlyin the case of
setter=).
getter=getterName
Specifies the name of the get accessor for the property. The getter must return a type matching the property’s type and take no parameters.
setter=setterName
Specifies the name of the set accessor for the property. The setter method must take a single parameter of a type matching the property’s type and must return
void.
If you specify that a property is
readonlyand also specify a setter with
setter=, you get a compiler warning.
Typically you should specify accessor method names that are key-value coding compliant (see
Key-Value Coding Programming Guide)—a common reason for using the
getterdecorator is to adhere to the
isPropertyName convention for Boolean values.
Writability
These attributes specify whether or not a property has an associated set accessor. They are mutually exclusive.readwrite
Indicates that the property should be treated as read/write. This attribute is the default.
Both a getter and setter method are required in the
@implementationblock. If you use the
@synthesizedirective in the implementation block, the getter and setter methods are synthesized.
readonly
Indicates that the property is read-only.
If you specify
readonly, only a getter method is required in the
@implementationblock. If you use the
@synthesizedirective in the implementation block, only the getter method is synthesized. Moreover, if you attempt to assign a value using the dot syntax, you get a compiler error.
Setter Semantics
These attributes specify the semantics of a set accessor. They are mutually exclusive.strong
Specifies that there is a strong (owning) relationship to the destination object.
weak
Specifies that there is a weak (non-owning) relationship to the destination object.
If the destination object is deallocated, the property value is automatically set to
nil.
(Weak properties are not supported on OS X v10.6 and iOS 4; use
assigninstead.)
copy (建立一个索引计数为1的对象,然后释放旧对象,它是值的拷贝,引用计数是不会再加1的.使用copy 针对NSString)
Specifies that a copy of the object should be used for assignment.
The previous value is sent a
releasemessage.
The copy is made by invoking the
copymethod. This
attribute is valid only for object types, which must implement the
NSCopying
protocol.
assign (简单的赋值,不更改索引计数.使用assign是针对基础的数据类型,比如NSinterger,CGFloat和c数据类型(int float double char))
Specifies that the setter uses simple assignment. This attribute is the default.
You use this attribute for scalar types such as
NSIntegerand
CGRect.
retain (retain释放旧对象,将旧对象的值赋予新对象,再增加新对象索引计数为1,它只是指针地址的复制,必须release释放;使用retain 一般是针对NSObject及子 类)
Specifies that
retainshould be invoked on
the object upon assignment.
The previous value is sent a
releasemessage.
In OS X v10.6 and later, you can use the
__attribute__keyword to specify that a Core Foundation property should be treated like an Objective-C object for memory management:
@property(retain) __attribute__((NSObject)) CFDictionaryRef myDictionary; |
Atomicity
You can use this attribute to specify that accessor methods are not atomic. (There is no keyword to denote atomic.)nonatomic
Specifies that accessors are nonatomic. By default, accessors are atomic.
Properties are atomic by default so that synthesized accessors provide robust access to properties in a multithreaded environment—that is, the value returned from the getter or set via the setter is always fully retrieved or set regardless of what other
threads are executing concurrently.
If you specify
strong,
copy, or
retainand do not specify
nonatomic, then in a reference-counted environment, a synthesized get accessor for an object property uses a lock and retains and autoreleases the returned value—the implementation will be similar to the following:
相关文章推荐
- js的Prototype属性 解释及常用方法
- js的Prototype属性 解释及常用方法
- js的Prototype属性 解释及常用方法
- js的Prototype属性解释及常用方法
- js中Prototype属性解释及常用方法
- css 常用布局属性解释
- js的Prototype属性解释及常用方法
- js中Prototype属性解释及常用方法
- js的Prototype属性 解释及常用方法
- js的Prototype属性 解释及常用方法
- js的Prototype属性 解释及常用方法
- js中常用元素属性scrollLeft,scrollWidth,clientWidth,offsetWidth的解释
- js中Prototype属性解释及常用方法
- 《Windows Form》Form常用属性解释
- 谈谈js中的prototype及prototype属性解释和常用方法
- js的Prototype属性 解释及常用方法(转载)
- 篇四、布局和控件的常用属性解释 一
- 转载: js的Prototype属性 解释及常用方法
- ThinkDev.Logging-Target常用属性解释
- js的Prototype属性 解释及常用方法