iOS代码规范
1、关于命名
UIButton *settingsButton;
大驼峰式命名:每个单词的首字母都采用大写字母 MFHomePageViewController
后缀要求:
ViewController: 使用ViewController做后缀
UITableCell:使用Cell做后缀
Protocol: 使用Delegate或者DataSource作为后缀
@property (nonatomic, copy) NSString *userName;
宏命名:
全部大写,单词间用 _ 分隔 [不带参数] //#define THIS_IS_AN_MACRO @"THIS_IS_AN_MACRO"
以字母 k 开头,后面遵循大驼峰命名 [不带参数] //#define kWidth self.frame.size.width
小驼峰命名 [带参数] //#define getImageUrl(url) [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kBaseUrl,url]]
枚举命名:
Enum类型的命名与类的命名规则一致
typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus){}
2、私有方法及变量声明
#import "CodeStandardViewController.h"
// 在这个category(类目)中定义变量和方法
@interface CodeStandardViewController ()
{
// 声明私有变量
}
// 私有方法
- (void)samplePrivateMethod;
@end
@implementation CodeStandardViewController
// 私有方法的实现
- (void)samplePrivateMethod
{
//doSth
}
3、关于注释
/**
* @brief 登录验证
*
* @param personId 用户名
* @param password 密码
* @param complete 执行完毕的block
*
* @return
*/
+ (void)loginWithPersonId:(NSString *)personId password:(NSString *)password complete:(void (^)(CheckLogon *result))complete;
4、格式化代码
1.定义一个对象时,指针 "" 靠近变量 NSString *userName;
2.Method与Method之间空一行:
#pragma mark - private methods
- (void)samplePrivateMethod
{...}
- (void)sampleForIf
{...}
3.大括号写法:
- (id)initWithNibName:(NSString *)nibNameOrNilbundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNilbundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (instancetype)init {
self = [super init];
if (self) {
// ...
}
return self;
}
4.左括号跟在第一行后边:用于在条件语句后面
if (someCondition) {
//Do something here
} else {
//Do something else
}
5、代码组织
#pragma mark - Lifecycle
- (instancetype)init {}
- (void)dealloc {}
- (void)viewDidLoad {}
- (void)viewWillAppear:(BOOL)animated {}
- (void)didReceiveMemoryWarning {}
#pragma mark - Custom Accessors
- (void)setCustomProperty:(id)value {}
- (id)customProperty {}
#pragma mark - IBActions
- (IBAction)submitData:(id)sender {}
#pragma mark - Public
- (void)publicMethod {}
- (instancetype)initWithWidth:(CGFloat)width height:(CGFloat)height {};
6、字面量
NSArray *names = @[@"Brian", @"Matt", @"Chris", @"Alex", @"Steve", @"Paul"];
NSDictionary *productManagers = @{@"iPhone": @"Kate", @"iPad": @"Kamal", @"Mobile Web": @"Bill"};
NSNumber *shouldUseLiterals = @YES;
NSNumber *buildingStreetNumber = @10018;
7、常量
static CGFloat const RWTImageThumbnailHeight = 50.0;
static NSString * const RWTAboutViewControllerCompanyName = @"RayWenderlich.com";
8、布尔值
if (someObject) {}
if (![anotherObject boolValue]) {}
9、ViewController的写法
#import "FindViewController.h"
@interface FindViewController () <FindViewDelegate>
@property (nonatomic, strong) NSMutableArray *dataArrays;
@end
@implementation FindViewController
#pragma mark - LifeCycle
#pragma mark -
- (void)dealloc
{
STLog(@"界面销毁");
}
- (instancetype)init
{
self = [super init ];//当前对象self
if (self !=nil) {//如果对象初始化成功,才有必要进行接下来的初始化
STLog(@"界面初始化");
}
return self;//返回一个已经初始化完毕的对象;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setBaseUI:@"Stone带我飞" sideVal:@"" backIvName:@"icon_return_black" navC:[STUIKit colorC00] midFontC:[STUIKit colorC01] sideFontC:[STUIKit colorC00] ];
[self setUI];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
}
#pragma mark - Public Method
#pragma mark -
#pragma mark - Private Method
#pragma mark -
- (void)setUI {
[self.view addSubview:self.findView];
[self setMas];
}
- (void)setMas {
[self.findView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view).offset(StatusBarAndNaviBarH);
make.left.equalTo(self.view);
make.width.equalTo(@(ScreenW));
make.bottom.equalTo(self.view).offset(-TabbarSafeBottomM);
}];
}
- (void)printMyName:(NSString *)myName
{
NSAssert(myName != nil, @"名字不能为空!");
STLog(@"My name is %@.",myName);
}
#pragma mark - IB-Action
#pragma mark -
#pragma mark - Notice
#pragma mark -
#pragma mark - Delegate
#pragma mark -
- (void)toOperate {
[self printMyName:@"Stone"];
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFI_NAME_01 object:nil];
}
#pragma mark - lazy load
#pragma mark -
- (FindView *)findView
{
if (!_findView){
_findView = [[FindView alloc] init];
_findView.delegate = self; //将FindViewViewController自己的实例作为委托对象
}
return _findView;
}
- (NSMutableArray *)dataArrays
{
if (!_dataArrays) {
_dataArrays = [NSMutableArray array];
}
return _dataArrays;
}
@end