`

【Objective-C】Property and Synthesize

阅读更多

原文地址:http://blog.eddie.com.tw/2010/12/08/property-and-synthesize/
原创作者:
高見龍(中国台湾)

 

上篇,因為在類別裡,instance variable(以下簡稱ivar)預設是protected的,也就是說只有該類別以及它的子類別才能存取它,如果要給外部使用的話,則需要來幫它加個setter/getter。但每次只為了一個ivar就要寫一對的setter/getter也太麻煩了,在Objective-C 2.0之後加入了@property@synthesize的語法,讓這個工作簡單多了。借用上一篇的例子:

1
2
3
4
5
6
7
8
9
10
@interface Book : NSObject
{
  int price;
}

-(int) price;
-(void) setPrice: (int) p;
+(void) printBookInfo

@end

如果改用@property來寫:

1
2
3
4
5
6
7
8
9
@interface Book : NSObject
{
  int price;
}

@property int price;
+(void) printBookInfo;

@end

原來的setter/getter就可以省下來不用寫,然後在@implementation的部份則是使用@synthesize語法:

1
2
3
4
5
6
7
8
9
10
@implementation Book
// 這個@synthesize語法幫忙產生setter/getter
@synthesize price;

+(void) printBookInfo
{
  NSLog(@"Hello, This is a book");
}

@end

這裡的@synthesize price,其實就相當於自動產生了我們在上一篇寫的那一對setter/getter的程式碼:

1
2
3
4
5
6
7
8
9
-(int) price
{
  return price;
}

-(void) setPrice: (int) value
{
  price = value;
}

這樣程式碼就簡潔許多了。雖然@synthesize會自動幫忙產生setter/getter,但如果你想要寫自己的setter/getter,或是想要額外添加功能在裡面,只要照著它預設生成的setter/getter方法命名規則就可以了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// interface
@interface Book : NSObject
{
  int price;
}

@property int price;
+(void) printBookInfo;

@end

// implementation
@implementation Book

@synthesize price;

// 自定setter
-(void)setPrice:(int)p
{
  // 故意讓傳入值變2倍
  price = p * 2;
}

+(void)printBookInfo
{
  NSLog(@"Hello, This is a book");
}
@end

@synthesize雖然會自動幫你建立一對setter/getter,但還是會以你建立的為主。

另外,記得在別的程式語言裡可以用點”.”來存取物件的屬性嗎? Objective-C 2.0之後也可以這樣做了,直接看看語法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Book *b = [[Book alloc] init];

// 一般的setter用法
[b setPrice:300];

// 一般的getter用法
NSLog(@"the price of the book is %d", [b price]);

// Objective-C 2.0之後可使用點語法,它會自動呼叫setter並將值傳給它
b.price = 200;

// 這裡則是呼叫getter把值傳回來
NSLog(@"the price of the book is %d", b.price);

[b release];

這樣有比較習慣了嗎? 我相信很多人對Objective-C的方括號的語法很感冒,至於程式碼的可讀性就看個人了,方括號語法看久了也是滿習慣的。不過用點語法的時候要注意幾件事,例如你自己寫了一個setter,又在裡面用了點語法:

1
2
3
4
5
-(void) setPrice: (int) p
{
  // 如果這樣寫的話,會啟動setPrice這個method
  self.price = p;
}

然後..就變無窮迴圈了! 這點要特別注意一下。

還有個問題是其實這個點語法光從程式碼其實看不太出來到底是物件還是結構還是變數..再來看另一個例子:

1
2
3
CGPoint pos = player.position;
pos.x += acceleration.x * 10;
player.position = pos;

這是一段Cocos2D的語法,內容大意是說:「我要建立一個CGPoint變數叫做pos,而這個pos是由player這個CCSprite的position來的」。acceleration是一個UIAcceleration物件,取得acceleration的x的值,修改pos裡的x之後再把整個pos變數塞回去給player這個角色。

這段程式碼對老手來說大概會覺得很弱,覺得這大概是新手,還得要建一個暫存變數,遜! 要是他來寫根本就可以直接併成一行:

1
player.position.x += acceleration.x * 10;

但事實上這樣寫的話會丟出一個”lvalue required as left operand of assignment”的編譯錯誤。為什麼會這樣? 分段來看:

1
player.position.x

其實這種”點語法”是一種”語法糖衣(syntactic sugar)”,事實上它是:

1
[player position].x

這邊position是一個getter,可以讓你從player身上取得一個CGPoint型態的position(r-value),但因為並沒有把指定給某個變數(l-value),所以當你要想用setter把它寫回去的時候,這個值已經消失了。

感想:看來Objective-C不只語法不同,連一些習慣寫法也不同了,眉眉角角真多!

 

 

分享到:
评论

相关推荐

    Objective-C for Absolute Beginners iPhone, iPad, and Mac Programming Made Easy

    Using a hands-on approach, you'll learn how to think in programming terms, how to use Objective-C to construct program logic, and how to synthesize it all into working apps. Gary Bennett, an ...

    Objective-C for Absolute Beginners: iPhone and Mac Programming Made Easy

    Using a hands-on approach, you'll learn how to think in programming terms, how to use Objective-C to construct program logic, and how to synthesize it all into working apps. Gary Bennett, an ...

    Objective-C中的@property和@synthesize用法详解

    相信每个初学者对@property和@synthesize都感到非常的陌生,在此给大家分享下我的自己的理解,有不当之处,还望多多指教。详细说明文章在下面连接http://blog.csdn.net/comeontom/article/details/7455459

    Objective-C for Absolute Beginners(Apress,3ed,2016).pdf

    Using a hands-on approach, you’ll learn how to think in programming terms, how to use Objective-C to construct program logic, and how to synthesize it all into working apps. Gary Bennett, an ...

    破解Objective-C面试:笑到最后的技术攻略!.zip

    Objective-C、iOS开发、Mac OS X、编程语言、面向对象编程、内存管理、自动引用计数(ARC)、协议(protocol)、类扩展(category)、键值观察(KVO)、键值编码(KVC)、Block、Delegate模式、多态性、Singleton...

    Objective-c对象组装XML

    [map setObject:@"c" forKey:@"content"]; 或者 NSMutableArray *list = [[NSMutableArray alloc]init]; NSMutableDictionary *map1 = [[NSMutableDictionary alloc]init]; [map1 setObject:@"a1" forKey:@...

    objective-c小技巧

    objective-c小技巧 1. 使用@property和@synthesize声明一个成员变量,给其赋值是时要在前面加上"self.",以便调用成员变量的setmember方法。 直接调用成员变量并且给其赋值:member=[NSString stringWithFormat...

    Objective-c解析XML封装

    @property (nonatomic, retain) NSMutableString *currentResult; @property (nonatomic, retain) NSMutableDictionary *map; @property (nonatomic, retain) NSMutableArray *list; -(NSMutableDictionary *)...

    传智博客-Objective-C PPT

    本套PPT,拥有以下OC语言的内容: ...二、OC的特有语法(点语法、@property、@synthesize关键字、id、构造方法、分类、description、SEL) 三、内存管理 四、协议(protocol)代码块(block) 五、ARC 等等

    iOS开发中属性 property 和 synthesize 详解

    针对iOS开发中属性 property 和 synthesize 进行了详细介绍

    Python库 | synthesize-0.0.1-py3-none-any.whl

    资源分类:Python库 所属语言:Python 资源全名:synthesize-0.0.1-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    irrelon-synthesize:一个用于 JavaScript 模块的简单 getter setter 合成器

    自动创建简单的方法来获取和设置 JavaScript 模块的属性,类似于 iOS Objective-C 中的 @synthesize 指令。 用法 在这个例子中,我们在 MyClass 原型上创建了一个名为 name() 的 getter/setter 方法: var MyClass...

    3D-Synthesize3DviaDepthOrSil.zip

    3D-Synthesize3DviaDepthOrSil.zip,[CVPR 2017]通过建模多视图深度图或轮廓生成和重建三维形状,3D建模使用专门的软件来创建物理对象的数字模型。它是3D计算机图形的一个方面,用于视频游戏,3D打印和VR,以及其他...

    Android代码-Synthesize

    Synthesize Synthesize is an android library which can create layout images in background threads, services, etc without inflating them in activity or fragments. Synthesize Documentation

    SWAT assessment tool

    The second objective is to summarize and synthesize the model performance statistics and parameters published in these articles to provide a succinct guide to complement a previous SWAT model summary...

    DT系列伺服马达产品手册.pdf

    DT系列伺服马达产品手册pdf,DT系列伺服马达产品手册

    Electrochemical and Spectroscopic Behaviour of Bis(2-mercaptopyridine-N-oxide)oxovanadium(IV)

    Electrochemical and Spectroscopic Behaviour of Bis(2-mercaptopyridine-N-oxide)oxovanadium(IV) Electrochemical and Spectroscopic Behaviour of Bis(2-mercaptopyridine-N-oxide)oxovanadium(IV) Beatriz S...

    双(2-巯基吡啶-N-氧化物)氧钒(IV)的电化学和光谱行为

    Electrochemical and Spectroscopic Behaviour of Bis(2-mercaptopyridine-N-oxide)oxovanadium(IV) Electrochemical and Spectroscopic Behaviour of Bis(2-mercaptopyridine-N-oxide)oxovanadium(IV) Beatriz S...

    Senfore_DragDrop_v4.1

    "Ignore All" when the IDE complains that this or that property doesn't exist and finally save the forms. ------------------------------------------- 3. Getting started: -----------------------------...

Global site tag (gtag.js) - Google Analytics