黑马程序员 --- OC中@property和@synthesize的用法
来源:互联网 发布:com.cn是什么域名 编辑:程序博客网 时间:2024/06/05 10:04
OC中@property和@synthesize的用法
OC类中有成员变量和方法部分,里面的成员变量如果不用@property和@synthesize的话,我们就要这样比如_age的访问在.h中:
#import <Foundation/Foundation.h>@interface Person:NSObject{ int _age;}-(void)setAge:(int)age;-(int)getAge;@end
这两个方法的声明以及在.m中的实现部分如:
#import "Person.h"@implementation Person-(void)setAge:(int )age{ _age = age;}-(int)getAge{ return _age;}@end
由于OC中Xcode这个软件给我们做了一些工作省了我们一些开发时间,比如Xcode4.4以前@property 用来自动生成成员变量的get/set方法声明,如:@property int age;等于上面的get和set方法声明,@synthesize是在.m文件中定义set和get方法的实现,是不是省了好多时间,我们还可以给@synthesize a = _b; 表示用a的get和set方法,修改属性b的值;在xcode4.4以后,@property int age;类自动帮我们声明和实现,即先声明一个成员变量_age,再自动生成get/set方法的声明和get/set方法的实现,是不是很快,感觉挺人性化的。
在Person.h文件中
#import <Foundation/Foundation.h>@interface Person:NSObject@property int age;@end
在Person.m文件中:
#import "Person.h"@implementation Person@end
手动实现:1)如果手动实现了set方法,那么编译器就只生成get方法和成员变量;
2)如果手动实现了get方法,那么编译器就只生成set方法和成员变量;
3) 如果 set 和 get 方法都是手动实现的,那么编译器将不会生成成员变量,并且报错;一般我们只手动实现set方法。
Xcode 非常的智能,为我们做些节省开发时间的操作。
0 0
- 黑马程序员 --- OC中@property和@synthesize的用法
- OC中@property和@synthesize的用法
- 黑马程序员------OC基础-----@property和@synthesize
- 黑马程序员-------OC----@property和@synthesize
- 在OC中,property、synthesize和点语法的用法
- 黑马程序员-@property与@synthesize的用法
- 10、黑马程序员-OC语言@property @synthesize和id
- 黑马程序员 —OC(@property和@synthesize)
- 黑马程序员--OC基础--@property和@synthesize关键字介绍
- 黑马程序员-----@property和@synthesize
- 黑马程序员---@property和@synthesize
- 黑马程序员(OC特有的语法一@ property和synthesize)
- 黑马-OC语法-@property和@synthesize
- OC中的@property、@synthesize和@dynamic的用法
- OC学习之@property和@synthesize用法
- OC学习: @property和@synthesize用法概述
- 黑马程序员-我的第十篇学习日记:OC中点语法,@property和@synthesize,完整创建一个可用对象
- 黑马程序员之ios学习总结——09 OC语言的点语法、@property和@synthesize
- mac shell 设置dns
- [android deverlop 学习笔记]ProGuard代码混淆
- 解决ubuntu启动器启动应用从新图标启动问题
- thinkphp中数据查询注意点
- 石子归并
- 黑马程序员 --- OC中@property和@synthesize的用法
- C语言实现字符串分割(类似vb中split函数)
- 解决 RHEL 7/ CentOS 7/Fedora 出现Unit iptables.service failed to load
- Android:利用Style和include标签来减少布局中的重复代码
- PHP的readfile函数和file_get_contents函数错误: Unable to find the wrapper "https"
- A. Arrays
- POJ 1000 A+B Problem
- POJ 1260 Pearls(DP)
- swift详解之十-------------异常处理、类型转换 ( Any and AnyObject )