iPhone 開發(fā)前準(zhǔn)備 必學(xué)內(nèi)容
iPhone 開發(fā)深入理解 iPhone OS/SDK 與 Objective-C 2.0是本完也好介紹的內(nèi)容,工欲善其事,必先利其器。在開發(fā)iPhone應(yīng)用程序的時(shí)候,深入理解iPhone OS/SDK與Objective-C 2.0是很重要的。
iPhone OS
iPhone OS 由4個(gè)主要部分組成。下面簡(jiǎn)單地羅列一下它們的功能。
Cocoa Touch
窗口和視圖
事件管理
用戶接口
加速傳感器
照相機(jī)
Media
Core Graphics(2維圖形接口)
Core Animation(動(dòng)畫)
OpenGL
Core Audio(聲音)
OpenAL
Media Player(MPEG4,MP3)
Core Services
Address Book
Core Foundation
Core Location
CFNetwork(http,https,ftp,SSL,TLS)
網(wǎng)絡(luò)安全
SQLite(SQL數(shù)據(jù)庫(kù))
XML
Core OS
多線程
網(wǎng)絡(luò)應(yīng)用(BSD套接字)
文件系統(tǒng)
Bonjour(利用無(wú)線網(wǎng)絡(luò)連接其他機(jī)器)
iPhone SDK
iPhone SDK 中主要包含下列4個(gè)工具。
Xcode - 項(xiàng)目管理、代碼編輯、編譯、調(diào)試(IDE)
Interface Builder - GUI 設(shè)計(jì)
iPhone Simulator - 模擬器
Instrument - 性能測(cè)試、調(diào)整
實(shí)際開發(fā)的過程中,基本上是在使用 Xcode 與 Interface Builder 來(lái)進(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 中卻不能使用它。所以我們需要自己來(lái)管理內(nèi)存。Objective-C 的內(nèi)存管理方式與使用引用計(jì)數(shù)的方式,就是說(shuō)對(duì)象有一個(gè)計(jì)數(shù)器,引用對(duì)象一次,計(jì)數(shù)器加一,當(dāng)計(jì)數(shù)器為0的時(shí)候,該對(duì)象的內(nèi)存被釋放。
創(chuàng)建對(duì)象實(shí)例的時(shí)候(init,alloc)應(yīng)用計(jì)數(shù)加一,執(zhí)行過程中,別的對(duì)象如果需要該對(duì)象,需要用(retain)來(lái)引用它,這時(shí),該對(duì)象的應(yīng)用計(jì)數(shù)器加一。不需要對(duì)象的時(shí)候用(release)來(lái)釋放,這時(shí)引用計(jì)數(shù)器減一,當(dāng)計(jì)數(shù)器為0的時(shí)候,釋放該對(duì)象內(nèi)存。
- init,alloc - 計(jì)數(shù)器 +1
- retain - 計(jì)數(shù)器 +1
- release - 計(jì)數(shù)器 -1
另外如果不使用 retain,release,可以使用(autorelease)來(lái)自動(dòng)釋放對(duì)象。
容器
Objective-C 中的容器主要有以下3種:
數(shù)組
字典
Set
向容器中添加的內(nèi)容不能直接用 int 或 float,需要通過 NSNumber 等封裝類來(lái)實(shí)現(xiàn)。Objective-C 2.0 開始可以使用迭代子(Enumerator),來(lái)順序訪問容器中的元素。
Notification
Notification是消息通知的功能。具體使用 NSNotificationCenter 類。將需要接受通知的對(duì)象,方法,事件注冊(cè)到該類上。
歸檔(Archive)
歸檔是指將對(duì)象的內(nèi)存布局原樣地保存到文件系統(tǒng)上。同樣對(duì)應(yīng)的由文件中的數(shù)據(jù)生成對(duì)象叫做UnAchive。在 iPhone SDK 中使用 NSKeyedArchiver 和 NSKeyedUnarchiver 類來(lái)實(shí)現(xiàn)。
一般在程序結(jié)束的時(shí)候,保存當(dāng)前的狀態(tài),再次啟動(dòng)的時(shí)候UnAchive一下,就又回到了剛才退出時(shí)的狀態(tài)。下面是一個(gè)例子:
- // MyKeyedArchiver.h
- #import
- @interface NSKeyedArchiver (MyKeyedArchiver)
- - (void)encodeValueOfObjCType:(const char *)valueType at:(const void *)address;
- @end
- #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:valueType
- at:address];
- [self encodeObject:[NSData dataWithData:datas]];
- [arch release];
- }
- @end
- // MyKeyedUnarchiver.h
- #import
- @interface NSKeyedUnarchiver (MyKeyedUnarchiver)
- - (void)decodeValueOfObjCType:(const char *)valueType at:(void *)data;
- @end
- #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:valueType
- at:data];
- [unarch release];
- }
- @end
小結(jié):iPhone 開發(fā)深入理解 iPhone OS/SDK 與 Objective-C 2.0的內(nèi)容介紹完了,希望本文對(duì)你有所幫助!