盈软iOS代码规范

2018-01-29  本文已影响51人  走进科学

0.写在前面

1.命名规范

驼峰:userName
下划线:user_name
类名: UserModel.h
变量名:redView
方法名:- (void)doSomething
属性名:@property (nonatomic, copy) NSString *name;

=- 1.4后缀:对象后缀需要做到见后缀知类型

UIViewController: XXXViewController
UIView: XXXView
UILabel: XXXLabel
规范: info = information
       UI = userinterface
      msg = message
      dict = dictionary
不规范: psn = person

2.在类的头文件中尽量少引用其他头文件

.h
#import <Foundation/Foundation.h>
@class YSDog;
@interface YSPerson : NSObject

@property (nonatomic, copy) NSString *name;

@property (nonatomic, assign) NSUInteger age;

@property (nonatomic, strong) YSDog *dog;

@end

.m
#import "YSPerson.h"
#import "YSDog.h"

@implementation YSPerson

@end

2.编码规范

规范:NSString *str = @"123";
   NSArray *array = @[@1, @2, @3];
   array[1];

不规范:NSString *str = [NSString stringWithString:@"123"];
   NSArray *array = [NSArray arrayWithObjects:@1, @2, @3, nil];
   [array objectAtIndex:1];
规范:
//header file
extern NSString *const kNotificationString;

//implementation file
NSString *const  kNotificationString = @"Finish Download";

不规范:
#define kNotificationString @"Finish Download"
typedef NS_ENUM(NSInteger, YSPersonGender) {
    YSPersonGenderMale,
    YSPersonGenderFemale
};
@property (nonatomic, copy, readonly) NSArray *persons;
规范: a = b + c;
不规范: a=b+c;
#pragma mark - 生命周期

#pragma mark - 重写

#pragma mark - 初始化

#pragma mark - 方法

#pragma mark - 事件

#pragma mark - 网络请求

#pragma mark - 数据源

#pragma mark - 代理

#pragma mark - Get&Set

3.Xcode

以上目录可以根据功能再进行分级

4.Api

项目中所有Api均已单独模型形式进行封装.Api的写法请如下.BaseApi的代码请参考YSGeneral

#import "BaseApi.h"

@interface YSEditPasswordApi : BaseApi


/**
.h
 修改密码Api
 1.6 修改密码的接口(完成)

 @param verifyCode 验证码(选填)
 @param password 密码
 @param callback 回调
 */
- (void)editPasswordWithVerifyCode:(NSString *)verifyCode
                          password:(NSString *)password
                          callback:(ApiRequestCallBack)callback;


@end

.m

#import "YSEditPasswordApi.h"

@implementation YSEditPasswordApi

- (void)editPasswordWithVerifyCode:(NSString *)verifyCode
                          password:(NSString *)password
                          callback:(ApiRequestCallBack)callback{
    
    // 参数拼接
    self.params[kAPI_NAME] = @"shipinhuiyi.user.editPassword";
    self.params[@"verify_code"] = verifyCode;
    self.params[@"new_password"] = [HttpSign doMD5:password];
    
    // Api调用成功之后的外部回调
    self.callback = callback;
    
    [self doRequestWithSuccessBlock:^(id resultData) {
        // Api调用成功之后的内部回调
        
    }];
    
}

@end
上一篇下一篇

猜你喜欢

热点阅读