Declared Properties

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 is the default.

Both a getter and setter method will be required in the @implementation. If you use @synthesize 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. If you use @synthesize 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.

assign

Specifies that the setter uses simple assignment. This is the default.

You typically use this attribute for scalar types such as NSInteger and CGRect, or (in a reference-counted environment) for objects you don’t own such as delegates.

retain and assign are effectively the same in a garbage-collected environment.
retain

Specifies that retain should be invoked on the object upon assignment. (The default is assign.)

The previous value is sent a release message.

Prior to Mac OS X v10.6, this attribute is valid only for Objective-C object types (so you cannot specify retain for Core Foundation objects—see “Core Foundation”).

On Mac 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, as illustrated in this example:

@property(retain) __attribute__((NSObject)) CFDictionaryRef myDictionary;

copy

Specifies that a copy of the object should be used for assignment. (The default is assign.)

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. For further discussion, see “Copy.”

Different constraints apply depending on whether or not you use garbage collection:

*

If you do not use garbage collection, for object properties you must explicitly specify one of assign, retain or copy—otherwise you will get a compiler warning. (This encourages you to think about what memory management behavior you want and type it explicitly.)

To decide which you should choose, you need to understand Cocoa’s memory management policy (see Memory Management Programming Guide).
*

If you use garbage collection, you don't get a warning if you use the default (that is, if you don’t specify any of assign, retain or copy) unless the property's type is a class that conforms to NSCopying. The default is usually what you want; if the property type can be copied, however, to preserve encapsulation you often want to make a private copy of the object.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值