Property 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 @synthesize
directive 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 copy
you 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 are propertyName
and set
PropertyName
:
respectively—for example, given a property “foo”, the accessors would be foo
and setFoo:
.
The following attributes allow you to specify custom names instead.
They are both optional and can appear with any other attribute (except
for readonly
in 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 readonly
and 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 getter
decorator is to adhere to the is
PropertyName
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 @implementation
block. If you use the @synthesize
directive 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 @implementation
block. If you use the @synthesize
directive 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 assign
instead.)
copy
Specifies that a copy of the object should be used for assignment.
The previous value is sent a release
message.
The copy is made by invoking the copy
method. This attribute is valid only for object types, which must implement the NSCopying
protocol
.
assign
Specifies that the setter uses simple assignment. This attribute is the default.
You use this attribute for scalar types such as NSInteger
and CGRect
.
retain
Specifies that retain
should be invoked on the object upon assignment.
The previous value is sent a release
message.
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 retain
and 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:
[_internal lock]; // lock using an object-level lock
|
id result = [[value retain] autorelease];
|
[_internal unlock];
|
return result;
|
If you specify nonatomic
, a synthesized accessor for an object property simply returns the value directly.
分享到:
相关推荐
More iPhone Cool project 有了一定基础可以看着本书,提升之用。 高质量PDF, 建议收藏!
来自O'Reilly的iPhone Game Development 电子书和示例工程以及来自Apress的iPhone Game Projects 电子书和全部源码。
用objective C写的实现iPhone上的Pie Chart,Line Chart and Bar Chart的显示和选中,虽不很完美,但比起很多简单的Demo,功能要完善很多。
iphone X iphone 8 原型框 iphone X iphone 8 原型框
IOS应用源码——OpenCV-iPhone(Xcode project_04.07.2010).zip
iPhone狂:约会iPhone
Iphone开发系列源码——Iphone主题源码Iphone开发系列源码——Iphone主题源码Iphone开发系列源码——Iphone主题源码Iphone开发系列源码——Iphone主题源码Iphone开发系列源码——Iphone主题源码Iphone开发系列源码...
【工具】★★★★-iphone-static-library-project-template
iphoneos-arm iPhone 必备工具
在Windows中安装iPhone开发环境在Windows中安装iPhone开发环境
ios iphone 源码 iphone桌面截图代码
Iphone开发系列源码——iPhone版Wordpress源代码Iphone开发系列源码——iPhone版Wordpress源代码Iphone开发系列源码——iPhone版Wordpress源代码Iphone开发系列源码——iPhone版Wordpress源代码Iphone开发系列源码...
iphone3,1 shsh iphone4 5.1.1iphone3,1 shsh iphone4 5.1.1
iphone SDK 下载BT
iPhone4各版本shsh
这个版本是完整的,能够下载。 iphone开发的初级教程,如果你对开发iphone无从下手就看这本书吧。
Part II:Project Walkthrough Chapter 4:The Game Controller Chapter 5:Specifying a Pong Game Chapter 6:Coding a Pong Game Chapter 7:Adding the Game Controller Chapter 8:Improving the Design Chapter 9:...
iPhone开发实战 iPhone开发 iPhone iPhone4 iPhone开发实战 iPhone开发 iPhone iPhone4