iPhone开发进阶(1) --- 深入理解iPhone OS/SDK与Objective-C 2.0
工欲善其事,必先利其器。在開發(fā)iPhone應(yīng)用程序的時(shí)候,深入理解iPhone OS/SDK與Objective-C 2.0是很重要的。
iPhone OS
iPhone OS 由4個(gè)主要部分組成。下面簡單地羅列一下它們的功能。
Cocoa Touch- 窗口和視圖
- 事件管理
- 用戶接口
- 加速傳感器
- 照相機(jī)
- Core Graphics(2維圖形接口)
- Core Animation(動(dòng)畫)
- OpenGL
- Core Audio(聲音)
- OpenAL
- Media Player(MPEG4,MP3)
- Address Book
- Core Foundation
- Core Location
- CFNetwork(http,https,ftp,SSL,TLS)
- 網(wǎng)絡(luò)安全
- SQLite(SQL數(shù)據(jù)庫)
- XML
- 多線程
- 網(wǎng)絡(luò)應(yīng)用(BSD套接字)
- 文件系統(tǒng)
- Bonjour(利用無線網(wǎng)絡(luò)連接其他機(jī)器)
iPhone SDK
iPhone SDK 中主要包含下列4個(gè)工具。
- Xcode - 項(xiàng)目管理、代碼編輯、編譯、調(diào)試(IDE)
- Interface Builder - GUI 設(shè)計(jì)
- iPhone Simulator - 模擬器
- Instrument - 性能測試、調(diào)整
實(shí)際開發(fā)的過程中,基本上是在使用 Xcode 與 Interface Builder 來進(jìn)行的。調(diào)試則是使用模擬器或者實(shí)際設(shè)備。要注意的是在PC上模擬程序,由于PC的主頻,性能高于實(shí)際設(shè)備,所以不能只在模擬器上調(diào)試。除此之外,一些類,功能在模擬器上也是不能使用的,比如 NSDateCalendar 類,或者是照相機(jī)功能。
Objective-C 2.0
內(nèi)存管理
雖然 Objective-C 2.0 已經(jīng)支持了垃圾收集了,但是 iPhone OS 中卻不能使用它。所以我們需要自己來管理內(nèi)存。Objective-C 的內(nèi)存管理方式與使用引用計(jì)數(shù)的方式,就是說對象有一個(gè)計(jì)數(shù)器,引用對象一次,計(jì)數(shù)器加一,當(dāng)計(jì)數(shù)器為0的時(shí)候,該對象的內(nèi)存被釋放。
創(chuàng)建對象實(shí)例的時(shí)候(init,alloc)應(yīng)用計(jì)數(shù)加一,執(zhí)行過程中,別的對象如果需要該對象,需要用(retain)來引用它,這時(shí),該對象的應(yīng)用計(jì)數(shù)器加一。不需要對象的時(shí)候用(release)來釋放,這時(shí)引用計(jì)數(shù)器減一,當(dāng)計(jì)數(shù)器為0的時(shí)候,釋放該對象內(nèi)存。
- init,alloc - 計(jì)數(shù)器 +1
- retain - 計(jì)數(shù)器 +1
- release - 計(jì)數(shù)器 -1
另外如果不使用 retain,release,可以使用(autorelease)來自動(dòng)釋放對象。
容器
Objective-C 中的容器主要有以下3種:
- 數(shù)組
- 字典
- Set
向容器中添加的內(nèi)容不能直接用 int 或 float,需要通過 NSNumber 等封裝類來實(shí)現(xiàn)。Objective-C 2.0 開始可以使用迭代子(Enumerator),來順序訪問容器中的元素。
Notification
Notification是消息通知的功能。具體使用 NSNotificationCenter 類。將需要接受通知的對象,方法,事件注冊到該類上。
歸檔(Archive)
歸檔是指將對象的內(nèi)存布局原樣地保存到文件系統(tǒng)上。同樣對應(yīng)的由文件中的數(shù)據(jù)生成對象叫做UnAchive。在 iPhone SDK 中使用 NSKeyedArchiver 和 NSKeyedUnarchiver 類來實(shí)現(xiàn)。
一般在程序結(jié)束的時(shí)候,保存當(dāng)前的狀態(tài),再次啟動(dòng)的時(shí)候UnAchive一下,就又回到了剛才退出時(shí)的狀態(tài)。下面是一個(gè)例子:
| 1 2 3 4 5 6 7 8 | // MyKeyedArchiver.h #import <Cocoa/Cocoa.h>@interface NSKeyedArchiver (MyKeyedArchiver)- (void)encodeValueOfObjCType:(const char *)valueType at:(const void *)address;@end |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #import "MyKeyedArchiver.h"@implementation NSKeyedArchiver (MyKeyedArchiver)- (void)encodeValueOfObjCType:(const char *)valueType at:(const void *)address {NSMutableData *datas = [NSMutableData data];NSArchiver *arch = [[NSArchiver alloc] initForWritingWithMutableData:datas];[arch encodeValueOfObjCType:valueTypeat:address];[self encodeObject:[NSData dataWithData:datas]];[arch release]; }@end |
| 1 2 3 4 5 6 7 8 | // MyKeyedUnarchiver.h #import <Cocoa/Cocoa.h>@interface NSKeyedUnarchiver (MyKeyedUnarchiver)- (void)decodeValueOfObjCType:(const char *)valueType at:(void *)data;@end |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #import "MyKeyedUnarchiver.h"@implementation NSKeyedUnarchiver (MyKeyedUnarchiver)- (void)decodeValueOfObjCType:(const char *)valueType at:(void *)data {NSData *datas = [self decodeObject];NSUnarchiver *unarch = [[NSUnarchiver alloc] initForReadingWithData:datas];[unarch decodeValueOfObjCType:valueTypeat:data];[unarch release]; }@end |
轉(zhuǎn)載于:https://www.cnblogs.com/dongxiaoyu/archive/2012/02/03/2337048.html
總結(jié)
以上是生活随笔為你收集整理的iPhone开发进阶(1) --- 深入理解iPhone OS/SDK与Objective-C 2.0的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: memcache的深度解析(转)
- 下一篇: iPhone应用程序编程指南(窗口和视图