oc——类——属性

概述

property是oc提供的代码生成机制(代码缩写机制),目的为了提高开发效率(节省代码敲入量)

property

@property

@property出现在interface中,用来合成Method声明,因此置于interface的{}外
@interface FBAnimal : NSObject

@property int age;
@property UIColor *color;

@end
本质就是在interface中合成以下Method声明
@interface FBAnimal : NSObject

- (int)age;
- (void)setAge:(int)age;
- (UIColor *)color;
- (void)setColor:(UIColor *)color;

@end
property总结:
  • 注:如果interface中已经显式声明property的getter和setter,@property不再合成对应的getter和setter,否则Method重复声明

@synthesize

@synthesize出现在implementation中,用来合成Method定义,因此置于implementation的{}外
@implementation FBAnimal

@synthesize age;
@synthesize color;

@end
本质就是在implementation中合成以下Method定义
@implementation FBAnimal
{
    int age;
    UIColor *color;
}

- (int)age
{
    //implement code
}

- (void)setAge:(int)age
{
    //implement code
}

- (UIColor *)color
{
    //implement code
}

- (void)setColor:(UIColor *)color
{
    //implement code
}

@end
@synthesize 属性名 [= 数据成员名],合成property对应的数据成员及getter和setter,数据成员名在”[= 数据成员名]“中指定,如果omit"[= 数据成员名]",则合成的数据成员名与属性名一致
在xcode 4.5中,引入了auto synthesize机制,可进一步omit @synthesize书写,也是代码生成机制(代码缩写机制),目的也是为了提高开发效率(节省代码敲入量),编译器替开发者合成”@synthesize 属性名 = 数据成员名“,在auto synthesize机制下,合成的数据成员名为"_属性名"(这也是数据成员名习惯约定以"_"起始的原因),因此在auto synthesize机制下,相当于合成以下代码
@synthesize age = _age;
@synthesize color = _color;
进一步合成的代码为
@implementation FBAnimal
{
    int _age;
    UIColor *_color;
}

- (int)age
{
    //implement code
}

- (void)setAge:(int)age
{
    //implement code
}

- (UIColor *)color
{
    //implement code
}

- (void)setColor:(UIColor *)color
{
    //implement code
}

@end
synthesize(auto synthesize)总结:
  • 如果interface中已经显式定义同名数据成员,synthesize(auto synthesize)不再合成对应的数据成员,否则数据成员重复定义,但interface中显式定义的数据成员类型必须与property类型一致,否则编译error
  • 如果implementation中已经显式定义property的getter和setter,synthesize(auto synthesize)不再合成对应的getter和setter,否则Method重复定义
  • 如果implementation中同时显式定义property的getter和setter,在synthesize下,仍会合成对应的数据成员(@synthesize显式合成),在auto synthesize下,不再合成对应的数据成员,编译器认为开发者要完全定制property的getter和setter,不再需要auto synthesize帮助

总结

  • 属性名也是标识符,遵守标识符命名规则
  • property会合成3种产物:数据成员,getter,setter
  • 数据成员名,synthesize下,开发者指定或默认(默认与属性名一致),auto synthesize下,_属性名
  • getter = 属性名,setter = set属性名(如果属性名以字母开头,属性名首字母大写)
  • 如果interface中已经显式声明property的getter和setter,@property不再合成对应的getter和setter,否则Method重复声明
  • 如果interface中已经显式定义同名数据成员,synthesize(auto synthesize)不再合成对应的数据成员,否则数据成员重复定义,但interface中显式定义的数据成员类型必须与property类型一致,否则编译error
  • 如果implementation中已经显式定义property的getter和setter,synthesize(auto synthesize)不再合成对应的getter和setter,否则Method重复定义
  • 如果implementation中同时显式定义property的getter和setter,在synthesize下,仍会合成对应的数据成员(@synthesize显式合成),在auto synthesize下,不再合成对应的数据成员,编译器认为开发者要完全定制property的getter和setter,不再需要auto synthesize帮助
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值