一個好的目錄結構,應該讓人一眼就看懂各層級的職責,同時具備應對變化的彈性。本文先盤點開發環境的關鍵路徑,再談一套實用且可長期維護的專案組織方式。
/Developer/Platforms
此目錄下通常有三個平台:
每個平台下都有 /Developer/usr/bin,放置開發所需工具,整體形如:
/Developer/Platforms/*/Developer/usr/bin/
其中 * 代表依目標平台而定的其中一個。
/Developer/Platforms/iPhoneSimulator.platform/Developer/Applications
裡面就是 iPhone Simulator.app。
// 真機 /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk // 模擬器 /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk
/var/mobile/Applications/4434-4453A-B453-4ADF4535345ADAF344
後面那串是 iOS 自動產生的識別碼,裡面固定包含:
AppDelegate Models Macro General Helpers Vendors Sections Resources
放應用入口檔,因為身分特殊,單獨一層。
放資料相關的 model:
Models |- BaseModel.h / .m |- CollectionModel.h / .m ...
放全域宏定義,依用途分檔:
Macro |- AppMacro.h |- NotificationMacro.h |- VendorMacro.h |- UtilsMacro.h ...
AppMacro.h 放 App 層級的常數:
// 表情相關 #define EMOTION_CACHE_PATH @"cachedemotions" #define EMOTION_RECENT_USED @"recentusedemotions" #define EMOTION_CATEGORIES @"categoryemotions" #define EMOTION_TOPICS @"emotiontopics" // 收藏相關 #define COLLECT_CACHE_PATH @"collected" // 瀑布流相關 #define WATERFALL_ITEM_HEIGHT_MAX 300 #define WATERFALL_ITEM_WIDTH 146
UtilsMacro.h 放提升效率的小工具:
#define UIColorFromRGB(r,g,b) [UIColor \
colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]
#define NSStringFromInt(intValue) [NSString stringWithFormat:@"%d", intValue]
VendorMacro.h 放第三方的 key:
#define UMENG_KEY @"xxxxx" #define UMENG_CHANNEL_ID @"xxx"
放會被重複使用的 Views/Classes/Categories:
General
|- Views
| |- TPKScollView
| |- TPKPullToRefresh
|- Classes
| |- TPKBaseViewController
| |- TPKHorizontalView
|- Categories
|- UIViewController+Sizzle
|- UIImageView+Downloader
放助手類,檔名直接對應功能:
Helpers |- TPKShareHelper |- TPDBHelper |- TPKEmotionHelper
助手類的主要價值是幫 Controller 減肥,同時提供一定程度的複用。
放第三方類庫與 SDK,例如 UMeng、WeiboSDK、WeixinSDK。
對應 App 的具體業務單元:
Sections |- Menu |- Setting |- Collection
放圖片等資源檔。
與業務無關的類庫用 Cocoapods 管理最省事,例如 SDWebImage、Reachability。另外像是多個 App 都會用到的基礎模塊(例如自公司的 API、社群分享、基礎工具層),可以各自建私有 git repo 再掛進 Podfile,更新時只要 pod update。
以基礎工具層為例,裡頭可以放那些「幾乎每個 App 都會用到」的小功能:例如每隔一段時間跳出求好評的 alert,封裝成 HBRating,使用時只需一行 [HBRating checkIfShouldPopupWithAppID:(NSInteger)appID];又例如推播註冊封裝成 APNS 類,各專案共用一份實作。
拿到設計稿後,第一步不是直接埋頭寫頁面,而是先抽離可複用的 Classes/Views/Helpers,思考每個效果的實作方式,並用合適的設計模式避免大量 if/else 巢狀。
一開始就鑽進 Sections 裡實作畫面,短期看起來好像比較快,但只要專案有一點複雜度,後期的維護成本會急遽上升。前期的準備工作值得花時間。