My IOS Engineering structure

Source: Internet
Author: User

A good architecture is not designed, but evolved!

Project structure

is an iOS project a common engineering structure, the overall model or in accordance with the structure of MVC, just in each layer to do some subdivision processing, the following is a brief introduction.

There is no very strict subcontracting mechanism like Java in iOS engineering, but we can also implement logical subcontracting in the project by group in the iOS project, which is more advantageous for us to organize and manage the code, make the engineering structure clearer and easier to understand.

Application: This group puts the appdelegate and some system constants and system configuration files;

Base: Some basic parent classes, including parent Viewcontroller and some common top-level custom parent classes, the classes of other modules generally inherit from some of the classes here;

controller: The system control layer, the placement of Viewcontroller, are inherited from the group base of Baseviewcontroller or Basetableviewcontroller;

View : The views layer in the system, because I prefer to implement the interface through the code, so here is the view that inherits from the UIView, I separated the view from the Viewcontroller and put it all here, This will keep the viewcontroller streamlined;

Model: an entity in a system that describes some of the roles and services in a system, and includes the processing logic corresponding to those roles and services;

Handler: The System business Logic layer, is responsible for processing the system complex business logic, the upper level caller is the Viewcontroller;

Storage: simple data storage, mainly a number of key values for storage and system external file access, including the Nsuserdefault and plist access to the package;

Network processing layer (rthttpclient), which encapsulates the afnetworking-based network processing layer, implements the callback of processing result by block, the upper caller is the handler layer;

Database : data layer, encapsulating SQLite database access and Management (Rtdatabasehelper) based on Fmdb, providing external call interface based on the model Layer object, encapsulating the stored procedure of data.

Utils: System Tool Class (Apputils), mainly put some system common tools class;

Categories: categories, extensions to existing system classes and custom classes;

Resource: resource library, including pictures, plist files, etc.;

Through the following login modules of the System class diagram, you can be more intuitive to see the full picture of this engineering structure.

The overall view is divided into three chunks, the yellow area of the model and the Business Logic layer (M), the blue area of the views layer (V), the red area of the View Controller layer (C), where the yellow region implements the business logic and data processing encapsulation, corresponding to their upper Viewcontroller, A very simple interface call can be implemented to extract the business complexity from the viewcontroller and to ensure the readability and maintainability of the Viewcontroller through a modular approach.

Keep Viewcontroller Simple

Often everyone complains that iOS Viewcontroller is getting bloated when it comes to writing, because with the complexity of the business and the increase in functionality, all the logic is contained in the Viewcontroller, Also includes some agent methods such as Uitableviewdatasource, make Viewcontroller bloated, maintainability is very low, coupling is also very high, in order to make viewcontroller easier, easy maintenance and follow-up development, To Viewcontroller thin body is particularly necessary, my approach mainly has three aspects.

1. The view view is separated from the Viewcontroller

If you use storyboard or xib This is of course, I prefer handwritten code, so not embedded in viewcontroller too much view layer code is to ensure Viewcontroller simple method, then, You can encapsulate the code for the view section separately into a subclass that inherits from UIView, and then implement the view and Viewcontroller communication through a custom delegate.

2. Separation of business logic and Viewcontroller

Putting network request processing and complex business logic and data access to the handler layer alone exposes only simple calling interfaces and callbacks implemented through blocks or delegate, which not only enables our engineering to be modular, It can also greatly reduce the complexity of viewcontroller, and there will be no lengthy Viewcontroller code that includes both network processing and data processing. Handler The result is processed by a block or delegate to the Viewcontroller,viewcontroller and then associated with the View view layer, which really plays the role of MVC, and the overall principle is that Let Viewcontroller only relate and be responsible for dealing with things related to it.

In BaseHandler.h, you can define some simple business processing rules:

#import <Foundation/Foundation.h>

/**

* Block called after handler processing is completed

*/

typedef VOID (^completeblock) ();

/**

* block that is called when handler processing succeeds

*/

typedef VOID (^successblock) (id obj);

/**

* block that is called when handler processing fails

*/

typedef VOID (^failedblock) (id obj);

@Interface Basehandler:nsobject

/**

* GET request URL

*

* @param path

* @return the assembled URL

*/

+ (NSString *) Requesturlwithpath: (NSString *) path;

@end

In Loginhandler, it is possible to define a calling interface for Loginviewcontroller exposure, encapsulating the responsible network processing and business processing logic in Loginhandler, and for Loginviewcontroller, Just call this method and pass in the corresponding Userentity entity object and handle the callback block on the success and failure states.

#import "BaseHandler.h"

#import "UserEntity.h"

@Interface Loginhandler:basehandler

/**

* User Login business logic processing

*

* @param user

* @param success

* @param failed

*/

-(void) Executelogintaskwithuser: (userentity *) User success: (Successblock) Success failed: (Failedblock) failed;

@end

3, DataSource or delegate and Viewcontroller separation

UITableView, often used in iOS development, contains a series of proxy methods that are often one of the culprits that make viewcontroller grow more complicated, so separating these datasource or delegate is one of the most effective ways. , for example, by customizing the DataSource Class (Implementing the Uitableviewdatasource Protocol) to centralize UITableView-related data source processing proxy methods into a specific class, Viewcontroller only need to set this custom data source class to UITableView, and then the rest can be handed to the custom data source class to handle.

My IOS Engineering structure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.