IOS中"声明属性"和类成员变量的关系
2012-11-04 18:03
405 查看
用apple的官方文档《The Objective-C Programming Language》
来说明这个问题:
参见Declared Properties章节:
@property
You can think of a property declaration as being equivalent to declaring two accessor methods. Thus
is equivalent to:
@synthesize
You use the @synthesize directive to tell the compiler that it should
synthesize the setter and/or getter
methods for a property if you do not supply them within the @implementation block.
The @synthesize
directive also synthesizes an appropriate instance variable if it is not otherwise declared.
同时呢,也复习了下如何使用属性:
1.Property Redeclaration
2.Core Foundation
3.Subclassing with Properties
you could define a class MyInteger
with a readonly property, value:
You could then implement a subclass, MyMutableInteger, which redefines the property to make it writable:
来说明这个问题:
参见Declared Properties章节:
@property
You can think of a property declaration as being equivalent to declaring two accessor methods. Thus
@property float value;
is equivalent to:
- (float)value; - (void)setValue:(float)newValue;
@synthesize
You use the @synthesize directive to tell the compiler that it should
synthesize the setter and/or getter
methods for a property if you do not supply them within the @implementation block.
The @synthesize
directive also synthesizes an appropriate instance variable if it is not otherwise declared.
@interface MyClass : NSObject @property(copy, readwrite) NSString *value; @end @implementation MyClass @synthesize value; @end
同时呢,也复习了下如何使用属性:
1.Property Redeclaration
// public header file @interface MyObject : NSObject @property (readonly, copy) NSString *language; @end // private implementation file @interface MyObject () @property (readwrite, copy) NSString *language; @end @implementation MyObject @synthesize language; @end
2.Core Foundation
@interface MyClass : NSObject @property(readwrite) CGImageRef myImage; @end @implementation MyClass @synthesize myImage; @end
3.Subclassing with Properties
you could define a class MyInteger
with a readonly property, value:
@interface MyInteger : NSObject @property(readonly) NSInteger value; @end @implementation MyInteger @synthesize value; @end
You could then implement a subclass, MyMutableInteger, which redefines the property to make it writable:
@interface MyMutableInteger : MyInteger @property(readwrite) NSInteger value; @end @implementation MyMutableInteger @dynamic value; - (void)setValue:(NSInteger)newX { value = newX; } @end
相关文章推荐
- iOS 成员变量和属性的关系
- iOS开发中 类、对象、实例变量、成员变量、属性变量等区别和关系
- IOS开发中 类、对象、实例变量、成员变量、属性变量等区别和关系
- iOS中属性与成员变量的区别
- iOS成员变量、实例变量、属性变量三者的联系与区别
- iOS中属性与成员变量的区别
- iOS中属性与成员变量的区别
- IE中,JS在操作style属性时报出"找不到成员"错误
- C# 引用其他命名空间的公共变量时的警告:由于"***"是引用封送类的字段,访问上面的成员可能导致运行时异常
- iOS运行时的用途三 -- 获得属性名,方法名,成员变量名
- iOS:属性、修饰词(内存管理) 及其对应成员变量 、ARC
- 关于iOS Category实现添加属性及成员变量
- iOS类别(category)不能添加成员变量但是可以添加属性的问题
- iOS 在类实现定义中声明成员变量的怪异方式
- 【原】iOS动态性(一):动态添加属性的方法——关联(e.g. 向Category添加成员变量)
- iOS中属性与成员变量的区别
- iOS学习笔记56(Runtime)-Objective-C Runtime 运行时之二:成员变量与属性
- iOS中属性与成员变量的区别
- ios开发-成员变量,属性变量随笔
- iOS中属性与成员变量的区别