Targets
http://blog.sina.com.cn/s/blog_682dc7810100pv8t.html
http://www.shangxueba.com/jingyan/1843732.html
在xcode當(dāng)中有一個(gè)東西叫targets,蘋果的官方文檔是這樣說(shuō)的:
A target specifies a product to build and contains the instructions for building the product from a set of files in a project or workspace. A target defines a single product; it organizes the inputs into the build system—the source files and instructions for processing those source files—required to build that product. Projects can contain one or more targets, each of which produces one product.(省略若干字)
簡(jiǎn)單翻譯過(guò)來(lái)就是一個(gè)target詳細(xì)說(shuō)明了要構(gòu)建的產(chǎn)品,包含了一個(gè)工程或工作目錄中的文件當(dāng)中的指令,貌似還是不太清楚。在谷歌搜了一下,發(fā)現(xiàn)這個(gè)關(guān)于target的解釋還是不錯(cuò)的:
A target basically defines what it is you are building and how you are building it. You can add more targets if you would like to build more than one thing. This usually makes sense if you need to build several related things from the same project. For instance, you might want one target for a full, paid version of an application, and another target for a reduced, free version of an application. Both targets would include much of the same code and resources, but some of the settings would be different and you might have different files included with each.
意思就是如果你想將一個(gè)應(yīng)用分為付費(fèi)完整版和免費(fèi)簡(jiǎn)化版,這兩個(gè)版本大部分代碼是一樣的,只有個(gè)別的設(shè)置和包含的文件不同,你就可以建一個(gè)application然后弄兩個(gè)targets分別對(duì)應(yīng)兩個(gè)版本。
下面舉個(gè)簡(jiǎn)單的例子來(lái)說(shuō)明在iOS7.0和iOS6.1(以及更低版本)之間的適配問(wèn)題(用的是xcode5.0,里邊有6.1和7.0兩個(gè)版本的sdk)
新建一個(gè)工程,默認(rèn)的development target,base sdk以及模擬器的版本都是7.0,在AppDelegate中的didFinishLaunchingWithOptions方法里寫下
self.window.tintColor = [UIColor redColor];
然后運(yùn)行,這樣是沒(méi)有任何錯(cuò)誤的。接下來(lái)將development target,base sdk以及模擬器的版本都改成6.1(注意默認(rèn)的xcode是沒(méi)有6.1的sdk的,需要自己另外導(dǎo)入)。然后運(yùn)行,就會(huì)報(bào)錯(cuò):

也就是說(shuō)tintColor屬性在iOS6.1中根本就沒(méi)有,在編譯時(shí)候就會(huì)出錯(cuò)。這時(shí)候如下加上判斷語(yǔ)句也是沒(méi)有用的,照樣報(bào)錯(cuò)
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {? ? ? ? self.window.tintColor = [UIColor redColor];? ? }
遇見這種情況只能加上預(yù)處理語(yǔ)句,這樣寫:
#ifdef __IPHONE_7_0? ? if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {? ? ? ? self.window.tintColor = [UIColor redColor];? ? }#endif
這樣編譯通過(guò)就不會(huì)報(bào)錯(cuò)了……這是因?yàn)樵趕dk6.1下的usr/include下邊有一個(gè)Availability.h文件,里邊定義了一大堆宏,其中關(guān)于iphone的有
#define __IPHONE_2_0? ? 20000#define __IPHONE_2_1? ? 20100#define __IPHONE_2_2? ? 20200#define __IPHONE_3_0? ? 30000#define __IPHONE_3_1? ? 30100#define __IPHONE_3_2? ? 30200#define __IPHONE_4_0? ? 40000#define __IPHONE_4_1? ? 40100#define __IPHONE_4_2? ? 40200#define __IPHONE_4_3? ? 40300#define __IPHONE_5_0? ? 50000#define __IPHONE_5_1? ? 50100#define __IPHONE_6_0? ? 60000#define __IPHONE_6_1? ? 60100#define __IPHONE_NA? ? ? 99999? /* not available */
而sdk7.0里邊多了一行
#define __IPHONE_7_0? ? 60100
Project
http://book.2cto.com/201307/27017.html
http://book.2cto.com/201307/26992.html
2.5 設(shè)置產(chǎn)品屬性
在前面講解應(yīng)用生命周期時(shí),為了禁止應(yīng)用在后臺(tái)運(yùn)行,我們將HelloWorld-Info.plist文件中的Application does not run in background屬性修改為YES(即UIApplicationExitsOnSuspend = YES),這項(xiàng)操作就屬于產(chǎn)品屬性的設(shè)置。在Xcode中,產(chǎn)品與Target直接相關(guān),而Target與Project直接相關(guān)。
2.5.1 Xcode中的Project和Target
打開HelloWorld工程時(shí),我們會(huì)看到如圖2-30所示的界面。產(chǎn)品屬性包括Project和Target兩塊內(nèi)容。一個(gè)工程只有一個(gè)Project,但可以有一個(gè)或多個(gè)Target。

圖2-30 Xcode的Project和Target
我們所創(chuàng)建的HelloWorld只有一個(gè)Target,下面我們?yōu)橹笆褂霉适掳鍖?shí)現(xiàn)的HelloWorld工程增加一個(gè)Target。
首先,依次選擇File→New→Target菜單項(xiàng),此時(shí)會(huì)彈出一個(gè)模板選擇對(duì)話框,如圖2-31所示。

圖2-31 選擇Target模板對(duì)話框
這里選擇的模板與新建工程時(shí)選擇的模板完全一樣,然后點(diǎn)擊Next按鈕,將出現(xiàn)如圖2-32所示的對(duì)話框。
根據(jù)情況逐一設(shè)定后,點(diǎn)擊Finish按鈕,現(xiàn)在我們已經(jīng)成功為HelloWorld新增了一個(gè)Target。查看左邊的導(dǎo)航面板,可以發(fā)現(xiàn)右邊有兩個(gè)Target,并同時(shí)生成一套完整的文件——main.m、AppDelegate、ViewController和MainStoryboard.storyboard,它們獨(dú)立于原來(lái)的Target而存在,如圖2-33所示。


圖2-32 Target的一些選項(xiàng)設(shè)定圖2-33 新創(chuàng)建Target
要指定運(yùn)行哪一個(gè)Target,可以通過(guò)選擇不同的Scheme來(lái)實(shí)現(xiàn)。如圖2-34所示,在Xcode的左上角選擇HelloWorld_Storyboard→iPhone 6.0 Simulator,就可以在iPhone 6.0 Simulator上運(yùn)行HelloWorld_ Storyboard Target了。

圖2-34 選擇Scheme