您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 教學 >> iOS 相關教學

Xcode iOS 專案目錄結構設計與開發流程建議

編輯:iOS 相關教學

一個好的目錄結構,應該讓人一眼就看懂各層級的職責,同時具備應對變化的彈性。本文先盤點開發環境的關鍵路徑,再談一套實用且可長期維護的專案組織方式。

一、開發環境常用路徑

1. 平台路徑

/Developer/Platforms

此目錄下通常有三個平台:

  • MacOSX.platform
  • iPhoneSimulator.platform
  • iPhoneOS.platform

每個平台下都有 /Developer/usr/bin,放置開發所需工具,整體形如:

/Developer/Platforms/*/Developer/usr/bin/

其中 * 代表依目標平台而定的其中一個。

2. 模擬器路徑

/Developer/Platforms/iPhoneSimulator.platform/Developer/Applications

裡面就是 iPhone Simulator.app。

3. SDK 路徑

// 真機
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk

// 模擬器
/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk

4. App 安裝路徑(真機)

/var/mobile/Applications/4434-4453A-B453-4ADF4535345ADAF344

後面那串是 iOS 自動產生的識別碼,裡面固定包含:

  1. App 本體,例如 test.app
  2. Documents:使用者資料,可同步到 iCloud
  3. Library:含 Caches(快取)與 Preferences(存放 NSUserDefaults 的 .plist)
  4. tmp:暫存檔

二、推薦的目錄結構

AppDelegate
Models
Macro
General
Helpers
Vendors
Sections
Resources

AppDelegate

放應用入口檔,因為身分特殊,單獨一層。

Models

放資料相關的 model:

Models
 |- BaseModel.h / .m
 |- CollectionModel.h / .m
 ...

Macro

放全域宏定義,依用途分檔:

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"

General

放會被重複使用的 Views/Classes/Categories:

General
 |- Views
 |   |- TPKScollView
 |   |- TPKPullToRefresh
 |- Classes
 |   |- TPKBaseViewController
 |   |- TPKHorizontalView
 |- Categories
     |- UIViewController+Sizzle
     |- UIImageView+Downloader

Helpers

放助手類,檔名直接對應功能:

Helpers
 |- TPKShareHelper
 |- TPDBHelper
 |- TPKEmotionHelper

助手類的主要價值是幫 Controller 減肥,同時提供一定程度的複用。

Vendors

放第三方類庫與 SDK,例如 UMeng、WeiboSDK、WeixinSDK。

Sections

對應 App 的具體業務單元:

Sections
 |- Menu
 |- Setting
 |- Collection

Resources

放圖片等資源檔。

三、用 Cocoapods 管理依賴

與業務無關的類庫用 Cocoapods 管理最省事,例如 SDWebImage、Reachability。另外像是多個 App 都會用到的基礎模塊(例如自公司的 API、社群分享、基礎工具層),可以各自建私有 git repo 再掛進 Podfile,更新時只要 pod update。

以基礎工具層為例,裡頭可以放那些「幾乎每個 App 都會用到」的小功能:例如每隔一段時間跳出求好評的 alert,封裝成 HBRating,使用時只需一行 [HBRating checkIfShouldPopupWithAppID:(NSInteger)appID];又例如推播註冊封裝成 APNS 類,各專案共用一份實作。

四、開發流程建議

拿到設計稿後,第一步不是直接埋頭寫頁面,而是先抽離可複用的 Classes/Views/Helpers,思考每個效果的實作方式,並用合適的設計模式避免大量 if/else 巢狀。

一開始就鑽進 Sections 裡實作畫面,短期看起來好像比較快,但只要專案有一點複雜度,後期的維護成本會急遽上升。前期的準備工作值得花時間。

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