@property和@synthesize 用法
来源:互联网 发布:新闻联播软件 编辑:程序博客网 时间:2024/05/29 14:27
声明部分
@property:自动生成设值和取值方法
实现部分
@synthesize 指令:告诉编译器 为属性生成一对设值方法和取值方法
假如有一个x的属性,则通过@synthesize 会自动实现一个取值方法x,一个设值方法setX
@interface Fraction:NSObject@property int numerator,denominator;-(void) print;-(void) convertToNum;@end
#import "Fraction.h"@implementation Fraction@synthesize numerator,denominator;-(void) print{ NSLog(@"%i/%i",numerator,denominator);}-(double) convertToNum{ if (denominator!=0) { return (double) numerator/denominator; }else{ return NAN; }}
int main(int argc, char const *argv[]){ @autoreleasepool{ Fraction *myFraction = [[Fraction alloc] init]; //访问变量 [myFraction numerator]; //或者 myFraction.numerator; //设值 [myFraction setNumberator: 5]; //或者 myFraction.numerator = 5; } return 0;}
那么为什么要让编译器自动生成?而不自己手动生成?
因为编译器自动生成的存取方法是高效的,并且在使用多个核心的多台机器上,使用多线程时也可正常运行。
0 0
- @property和@synthesize 用法
- @property和@synthesize用法
- Objective-C中的@property和@synthesize用法
- Objective-C中的@property和@synthesize用法
- Objective-C中的@property和@synthesize用法
- Objective-C中的@property和@synthesize用法
- Objective-C中的@property和@synthesize用法
- Objective-C中的@property和@synthesize用法
- Objective-C中的@property和@synthesize用法
- Objective-C中的@property和@synthesize用法
- Objective-C中的@property和@synthesize用法
- Objective-C中的@property和@synthesize用法
- Objective-C中的@property和@synthesize用法
- Objective-C中的@property和@synthesize用法
- Objective-C中的@property和@synthesize用法
- Objective-C中的@property和@synthesize用法
- Objective-C中的@property和@synthesize用法
- OC学习之@property和@synthesize用法
- ecshop 商品详细页出现读取信息有缓存 需要后台清除缓存才能正常 一段时间后产品显示又不正常的
- Xcode高级调试技巧5
- Android 自定义imageview 图片高度固定大小宽度按比例自适应
- 正则化方法:L1和L2 regularization、数据集扩增、dropout
- Firefox访问https出现 ssl_error_weak_server_ephemeral_dh_key错误
- @property和@synthesize 用法
- 【FAQ】MAC,ping IP能通,ping域名不通,为何?
- Netty使用实例
- A星算法
- Android开发一些边角数据
- 工业4.0核心技术----IEC61131-3
- IKEv1 MainMode Cert 代码流程梳理
- 如何修改dede文章页上一篇下一篇"没有了"
- Git 分支 - 分支的新建与合并