天天看點

Info.plist 和 pch 檔案

掌握Info.plist 和 pch 檔案的作用

  •  Info.plist常見的設定

 建立一個新工程,會在Supportting Files 檔案夾下看到一個“Info.plist“的檔案,該檔案對工程做一些運作期的配置,非常重要不能删除

Info.plist 和 pch 檔案

項目中其他Plist檔案不能帶有Info這個字眼,不然會被錯認為是傳說中非常重要的“Info.plist”

  •   常見屬性(紅色部分是用文本編輯器打開時看到的key)

Localiztion native development region( CFBundleDevelopmentRegion)-本地化相關   Bundle display name( CFBundleDisplayName)-程式安裝後顯示的名稱,限制在10-12個字元,如果超出,将被顯示縮寫名稱   Icon file( CFBundleIconFile)-app圖示名稱,一般為Icon.png   Bundle version( CFBundleVersion)-應用程式的版本号,每次往App Store上釋出一個新版本時,需要增加這個版本号   Main storyboard file base name( NSMainStoryboardFile)-主storyboard檔案的名稱   Bundle identifier( CFBundleIdentifier)-項目的唯一辨別,部署到真機時用到  

  • pch檔案

 pch頭檔案的内容能被項目中的其他所有源檔案共享和通路

一般在pch檔案中定義一些全局的宏,例如

//frame
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define k_frameWidth  self.frame.size.width
#define k_frameHeight self.frame.size.height
#define kScreen [UIScreen mainScreen].bounds
#define k_frame_X self.frame.origin.x
#define k_frame_Y self.frame.origin.y

//字型
#define k_boldFont(number)  [UIFont boldSystemFontOfSize:number]
#define k_sysFont(number)  [UIFont systemFontOfSize:number]
//顔色
#define kClolor(x,y,z,k) [UIColor colorWithRed:x/255.0  green:y/255.0 blue:z/255.0 alpha:k]
           

   在Xcode6之前,建立一個工程的時候,系統會幫我們自動建立一個以工程名為名字的pch (precompile header)檔案,在開發過程中,可以将那些整個工程都廣泛使用的頭檔案包含在該檔案下,編譯器就會自動的将pch檔案中的頭檔案添加到所有的源檔案中 去,這樣在需要使用相關類的時候不需要使用import就可以直接使用頭檔案中的内容,很大程度上帶來了程式設計的便利性,但潛在的也帶來了一些問題,這也是 在Xcode6中預設不再建立pch的原因吧。

那麼怎麼在Xcode6中添加一個pch檔案呢?

Info.plist 和 pch 檔案

首先,Command+N,打開建立檔案視窗:ios->other->PCH file,建立一個pch檔案,添加需要引入的頭檔案名:

Info.plist 和 pch 檔案

有時我們打開工程時,會報pch檔案路徑錯誤,這樣我們改下路徑,就OK拉。