Github.com/yinanwang1/…

1. Use cocoaPod to manage third-party libraries

Common third-party libraries

platform :ios, '7.0'

target 'Structure' do

pod 'AFNetworking'.'~ > 2.5.4'
pod 'SDWebImage'.'~ > 3.7.5'
pod 'JTObjectMapping'.'~ > 1.1.2'
pod 'XTSafeCollection'.'~ > 1.0.3'
pod 'Masonry'.'~ > 0.6.4'

end
Copy the code

The XTSafeCollection is recommended to avoid the crash caused by an array out of bounds.

2. The network layer

(1) AFNetworking encapsulation

Encapsulate Afnetworking

+ (AFHTTPSessionManager *)sharedClient;

+ (NSURLSessionDataTask *)postRequest:(NSString*)path
                           parameters:(id)parameters
                             encToken:(NSString*)encToken
                              isLogin:(BOOL)isLogin
                              success:(void (^)(ErrorCode status, NSString * msg, NSDictionary * data))success
                              failure:(void (^)(ErrorCode status, NSString *msg, NSDictionary * data))failure;

+ (NSURLSessionDataTask *)getRequest:(NSString*)path
                          parameters:(NSDictionary*)parameters
                            encToken:(NSString*)encToken
                             isLogin:(BOOL)isLogin
                             success:(void (^)(ErrorCode status, NSString * msg, NSDictionary * data))success
                             failure:(void (^)(ErrorCode status, NSString *msg, NSDictionary * data))failure;

+ (NSURLSessionDataTask *)uploadRequest:(NSString*)path
                             parameters:(NSDictionary*)parameters
                               encToken:(NSString*)encToken
                          formDataArray:(NSArray *)formDataArray
                                isLogin:(BOOL)isLogin
                                success:(void (^)(ErrorCode status, NSString * msg, NSDictionary * data))success
                                failure:(void (^)(ErrorCode status, NSString *msg, NSDictionary * data))failure;

+ (NSURLSessionDataTask *)putRequest:(NSString*)path
                          parameters:(NSDictionary*)parameters
                            encToken:(NSString*)encToken
                             isLogin:(BOOL)isLogin
                             success:(void (^)(ErrorCode status, NSString * msg, NSDictionary * data))success
                             failure:(void (^)(ErrorCode status, NSString *msg, NSDictionary * data))failure;

+ (NSURLSessionDataTask *)deleteRequest:(NSString*)path
                             parameters:(NSDictionary*)parameters
                               encToken:(NSString*)encToken
                                isLogin:(BOOL)isLogin
                                success:(void (^)(ErrorCode status, NSString * msg, NSDictionary * data))success
                                failure:(void (^)(ErrorCode status, NSString *msg, NSDictionary * data))failure;
Copy the code

Create an AFHTTPSessionManager in sharedClient, and encapsulate Get, Post, Upload, Put and Delete class methods, for basic HTTP request operations.

(2) The use of WebService

Assuming there is an interface #define URL_TEST @”test” for web fetching, create a chatsModel.h file that will essentially put all the interfaces of the Chats module into this file. Such as

- (void)fetchData:(void (^)(ErrorCode status, NSString *messageStr, ChatsEntity *chatesEntity))compelte;

(3) Entity transformation

Convert Json to Model using JTObjectMapping third-party library, Mantle is also available. Convert the data returned from the server directly to the Model. There are two instance variables in Model

@property (nonatomic, strong) NSString *nameStr;
@property (nonatomic, strong) NSNumber *ageIntNum;
Copy the code

Create a method to convert the type

+ (instancetype)createChatsEntityWithDic:(NSDictionary *)dic
{
    NSDictionary *mapping = @{
                              @"name": @"nameStr"The @"age": @"ageIntNum"}; ChatsEntity *entity = [ChatsEntity objectFromJSONObject:dic mapping:mapping];return entity;
}
Copy the code

3. Organizational structure

As for the organization, be sure to open the project’s folders, manually create folders for each layer, and then Add the folders to Project. This has the advantage that the folders displayed in Xcode are the same as those displayed in Finder. Otherwise the files you see in the Finder are all in the Structure folder, which is not easy to read.

(1) Macros

Macro definitions generally create four files: <1> url. h Defines the URL macros needed for network access in this file. For example, #define SERVER_URL @”http://www.baidu.com”. <2> keys. h place the required Key in this file, such as wechat, weibo, shareSDK, etc. <3> Macros.h places enumerations that are used in a project and are used globally in this file to make it easy to call and avoid repeated definitions. Such as

Typedef enum {// Unknown error kUnknownError = -1, kNoError = 0, kNetWorkError = 1,}ErrorCode;Copy the code

< 4 > UserDefaultAndNotification. H will notify and saved to the User Key in the Default save to this file, convenient and unified control Keys, avoid repetition definition exception error. <5>Utilities.h puts all defined tool macros in this file, common dlogs, etc.

(2) the MVCs

Business-related files are separated by modules, and are distinguished by MVC in each module, as shown in the following

(3) the Utilities

Will be in each module, need to use UIKit or algorithm, can be pulled out into a file, such as custom AlertView goods Share.

(4) Base

Define some basic classes like UIViewController, UINavigationController. Put common colors, load views, etc. in this folder according to APP requirements.

(5) the External

Some third-party library files are not stored on Cocoapod. Copy the files to this folder and do not modify the third-party library files. You can create a category of the third-party library to extend the functions.

(6) Resources

Put images, plist and other resource files.

(7) PrefixHeader. PCH

Put header files that are needed in many classes into this file. Avoid adding the same header file to each class file at once.

The code specification can be referred to the iOS Code Programming Specification – a summary of project experience.