您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 版本適配筆記(二):target 是什麼,以及 #ifdef \_\_IPHONE\_7\_0 的原理

編輯:iOS 開發綜合

繼續談不同 iOS 版本間的適配問題。要理解這件事,得先搞清楚 Xcode 裡 target 的概念。

target 到底是什麼

Apple 官方文件的說法是:

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.

直接翻譯有點拗口。網路上另一段解釋說得更清楚:

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.

白話就是:target 定義「你要做的是什麼、要怎麼做」。同一個專案可以有多個 target,各自產出一個產品。典型場景是付費完整版與免費精簡版——兩者共用大部分程式碼與資源,但設定不同、包含的檔案也不同,這時就建立兩個 target 分別對應。

動手實驗:tintColor 在 6.1 上的編譯錯誤

以下用 Xcode 5.0 示範(該版本內建 6.1 與 7.0 兩套 SDK)。

新建專案,此時 Development Target、Base SDK 與模擬器版本預設都是 7.0。在 application:didFinishLaunchingWithOptions: 中寫:

self.window.tintColor = [UIColor redColor];

執行,沒有任何錯誤。

接著把 Development Target、Base SDK 與模擬器都改成 6.1(注意預設的 Xcode 沒有 6.1 的 SDK,需自行匯入),再執行就會看到編譯錯誤:

也就是說 tintColor 這個屬性在 iOS 6.1 中根本不存在,編譯階段就找不到它。

這時候加執行期判斷是沒有用的,照樣編譯失敗:

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
    self.window.tintColor = [UIColor redColor];
}

原因很單純:編譯器在編譯階段就要解析每個符號,它不管你執行時會不會真的走到這一行。既然 6.1 的標頭檔裡沒有 tintColor,編譯就過不了。

正確做法是加上預處理指令:

#ifdef __IPHONE_7_0
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        self.window.tintColor = [UIColor redColor];
    }
#endif

這樣就通過了。

__IPHONE_7_0 從哪來

每套 SDK 的 usr/include 下有一個 Availability.h,定義了大量版本巨集。6.1 SDK 中與 iOS 相關的部分如下:

#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 */

而 7.0 的 SDK 多了一行:

#define __IPHONE_7_0   70000

所以 #ifdef __IPHONE_7_0 判斷的是「編譯時所用的 SDK 是否為 iOS 7.0 以上」。只有用 7.0 SDK 編譯時,這個巨集才會被定義,區塊內的程式碼才會進入編譯。

編譯期判斷與執行期判斷的分工

這兩者是搭配使用的,各自解決不同層次的問題:

  • #ifdef(編譯期):決定「這段程式碼要不要被編譯」。用來處理 API 是否存在於當前 SDK。
  • 執行期版本判斷:決定「這段已編譯的程式碼要不要執行」。用來處理使用者裝置的系統版本。

只有前者,會在舊系統裝置上執行到不存在的 API 而崩潰;只有後者,會在舊 SDK 下直接編譯失敗。上面範例把兩者並用,才是完整的寫法。

補充一個更簡潔的替代方案:若只是想確認某個物件能不能回應某個方法,用 [window respondsToSelector:@selector(setTintColor:)] 判斷,比比對版本號更精準,也不必依賴 SDK 巨集。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved