Xcode 项目组结构的最佳实践?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39945727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best practice for an Xcode project groups structure?
提问by AppsDev
In tutorials and examples that provide code samples, sometimes I see that project files in the Xcode's Project navigator are arranged by groups following the MVC pattern ("Views", "Controllers", "Model"), and other times they are organized into groups by functionality ("Login", "Checklist", for instance).
在提供代码示例的教程和示例中,有时我会看到 Xcode 的 Project navigator 中的项目文件是按照 MVC 模式(“视图”、“控制器”、“模型”)按组排列的,有时它们被组织成组按功能(例如“登录”、“清单”)。
Concerning iOS, is there any Apple's convention/recommendation for this? Which should be the best practice?
关于 iOS,是否有任何 Apple 对此的约定/建议?哪个应该是最佳实践?
回答by Jamshed Alam
Developers organise their groups, code, and files many ways. But I use something like the following:
开发人员以多种方式组织他们的组、代码和文件。但我使用类似以下内容:
CoreData: Contains DataModel and Entity Classes.
Extension: Contain One class(default apple class extensions+project class extensions.)
Helper: Contain Third Party classes/Frameworks (eg. SWRevealController) + Bridging classes (eg. Obj C class in Swift based project)
Model: Make a singleton class (eg.AppModel - NSArray,NSDictionary, String etc.) for saving data. The Web Service Response parsing and storing data is also done here.
Services: Contain Web Service processes (eg. Login Verification, HTTP Request/Response)
View: Contain storyboard, LaunchScreen.XIB and View Classes. Make a sub folder Cells - contain UITableViewCell, UICollectionViewCell etc.
Controller: Contain Logic or Code related to UIElements (eg. UIButton's reference+ clicked action)
CoreData:包含数据模型和实体类。
扩展:包含一个类(默认苹果类扩展+项目类扩展。)
助手:包含第三方类/框架(例如 SWRevealController)+ 桥接类(例如基于 Swift 的项目中的 Obj C 类)
模型:制作一个单例类(例如 AppModel - NSArray、NSDictionary、String 等)来保存数据。Web Service Response 解析和存储数据也在这里完成。
服务:包含 Web 服务进程(例如登录验证、HTTP 请求/响应)
View:包含 storyboard、LaunchScreen.XIB 和 View 类。创建一个子文件夹 Cells - 包含 UITableViewCell、UICollectionViewCell 等。
控制器:包含与 UIElements 相关的逻辑或代码(例如 UIButton 的引用+点击动作)
This structure is from another Stack Overflow post.
此结构来自另一篇 Stack Overflow 帖子。
These may also help you:
这些也可能对您有所帮助:
回答by Julian B.
I actually created a project to demonstrate what I consider my go-to Xcode project structure for a small or mid-sized code base. You can find it here.
我实际上创建了一个项目来演示我认为我的小型或中型代码库的首选 Xcode 项目结构。你可以在这里找到它。
Here's an outline of it:
这是它的概述:
- Source - All source code
- Account - Account-related classes (session-related classes, account logic, etc)
- Application - Application-related classes. App delegate, configuration classes, etc
- Core Additions - Extensions and subclasses stemming from apple's classes
- Utilities - General utility classes. Useful extensions, formatting utilities, convenience classes and such
- Element-based folders - Folder for UIView, UITableViewCell, etc
- Local Persistence - Local persistence layer. All interactions with local database (realm, core data)
- Repositories - All model-related local persistence logic
- Constants - All constants. URLs, fonts, colors, errors, etc
- Models - All models (server-side entities' representation). We would also throw here any object mapping logic
- Modules - Here we can find each of the application's pieces divided by functionality
- Module-based folders - Each folder contains all module-specific view controllers, views, delegates and related classes
- Networking - The app's networking layer (e.g. classes responsible for interacting with web services)
- Services - All model-related web logic
- Storyboards - Contains all storyboard files
- Resources - Any additionaly resources like media, documents, localization files and such
- 源 - 所有源代码
- 帐户 - 帐户相关类(会话相关类、帐户逻辑等)
- 应用程序 - 与应用程序相关的类。应用委托、配置类等
- 核心添加 - 源自苹果类的扩展和子类
- 实用程序 - 通用实用程序类。有用的扩展、格式化实用程序、便利类等
- 基于元素的文件夹 - UIView、UITableViewCell 等的文件夹
- 本地持久性 - 本地持久层。与本地数据库(领域、核心数据)的所有交互
- Repositories - 所有与模型相关的本地持久化逻辑
- 常量 - 所有常量。URL、字体、颜色、错误等
- 模型 - 所有模型(服务器端实体的表示)。我们还会在这里抛出任何对象映射逻辑
- 模块 - 在这里我们可以找到按功能划分的应用程序的每个部分
- 基于模块的文件夹 - 每个文件夹包含所有特定于模块的视图控制器、视图、委托和相关类
- 网络 - 应用程序的网络层(例如负责与网络服务交互的类)
- 服务 - 所有与模型相关的网络逻辑
- 故事板 - 包含所有故事板文件
- 资源 - 任何其他资源,如媒体、文档、本地化文件等