OC- @property @synthesize
2015-11-10 16:48
190 查看
@property
1,在@interface中
2,自动生成setter和getter的声明
#import <Foundation/Foundation.h> @interface Person : NSObject { int _age; // int age; int _height; double _weight; NSString *_name; } // @property:可以 自动生成某个成员变量的setter和getter声明 @property int age; //- (void)setAge:(int)age; //- (int)age; @property int height; //- (void)setHeight:(int)height; //- (int)height; - (void)test; @property double weight; @property NSString *name; @end
Person.m
#import "Person.h" @implementation Person // @synthesize自动生成age的setter和getter实现,并且会访问_age这个成员变量 @synthesize age = _age; @synthesize height = _height; @synthesize weight = _weight, name = _name; @end
#import "Person.h" @implementation Person // @synthesize自动生成age的setter和getter实现,并且会访问_age这个成员变量 @synthesize age = _age; @synthesize height = _height; @synthesize weight = _weight, name = _name; @end
@property关键字
自动生成某个成员变量的setter和getter方法的声明
相当于 - (void)setAge:(int)age;
- (int)age;
@synthesize
语法: @synthesize age = _age;
相当于:
- (void) setAge:(int)age
{
_age = age;
}
- (int)age
{
return _age;
}
如果成员变量_age不存在,就会自动生成一个@private的成员变量_age
若:@synthesize age;
setter和getter实现中会访问成员变量age
如果成员变量age不存在,就会自动生成一个@private的成员变量age
手动实现:
若手动实现了setter方法,编译器就只会自动生成getter方法
若手动实现了getter方法,编译器就只会自动生成setter方法
若同时手动实现了setter和getter方法,编译器就不会自动生成不存在的成员变量
新特性:
自从Xcode 4.x后, @property就独揽了@synthesize的功能
默认情况下,setter和getter方法中的实现,会去访问下划线_开头的成员变量
相关文章推荐
- 快速建立Linux c/c++编译环境
- linux 下 rmdir和rm的区别
- CentOS离线安装gcc4.8.2
- Simplygon Why do we optimize?
- 公司网站如何让用户使用QQ第三方登录
- linux awk数组操作详细介绍
- linux常用命令练习(vim相关)
- 04(maven+SSH)网上商城项目实战之maven热部署
- Squid反向代理
- CentOS Linux 升级内核步骤和方法
- Dubbo架构设计详解
- apache 网页301重定向、自定义400/403/404/500错误页面
- Linux常用命令
- 架构设计最佳实践与必备素养
- Apache Karaf自定义feature
- linux允许80端口通过
- shell编程之【告警系统】
- 常用linux命令
- opencv进行图像序列与视频的转换
- centos下安装RabbitMQ消息队列