1.为工程设置编码风格规范
1.setting and getting 方法
//For example
view.backgroundColor = [UIColor orangeColor];
[UIApplication sharedApplication].delegate;
//Not
[view setBackgroundColor:[UIColor orangeColor]];
UIApplication.sharedApplication.delegate;
2.间隙spacing
if (user.isHappy) {
// Do something
}
else {
// Do something else
}
3.if and else
//For example:
if (!error) {
return success;
}
//Not:
if (!error)
return success;
or
if (!error) return success;
4.Error Handling
For example:
NSError *error;
if (![self trySomethingWithError:&error]) {
// Handle Error
}
Not:
NSError *error;
[self trySomethingWithError:&error];
if (error) {
// Handle Error
}
5.方法 -/+ 后面加空格
- (void)setExampleText:(NSString *)text image:(UIImage *)image;
6.类属性创建
//For example:
@interface NYTSection: NSObject
@property (nonatomic) NSString *headline;
@end
//Not:
@interface NYTSection : NSObject {
NSString *headline;
}
7.Categories
//For example:
@interface UIViewController (NYTMediaPlaying)
@interface NSString (NSStringEncodingDetection)
//Not:
@interface NYTAdvertisement (private)
@interface NSString (NYTAdditions)
8.NSString, NSDictionary, NSArray, and NSNumber
//For example:
NSArray *names = @[@"Brian", @"Matt", @"Chris", @"Alex", @"Steve", @"Paul"];
NSDictionary *productManagers = @{@"iPhone" : @"Kate", @"iPad" : @"Kamal", @"Mobile Web" : @"Bill"};
NSNumber *shouldUseLiterals = @YES;
NSNumber *buildingZIPCode = @10018;
//Not:
NSArray *names = [NSArray arrayWithObjects:@"Brian", @"Matt", @"Chris", @"Alex", @"Steve", @"Paul", nil];
NSDictionary *productManagers = [NSDictionary dictionaryWithObjectsAndKeys: @"Kate", @"iPhone", @"Kamal", @"iPad", @"Bill", @"Mobile Web", nil];
NSNumber *shouldUseLiterals = [NSNumber numberWithBool:YES];
NSNumber *buildingZIPCode = [NSNumber numberWithInteger:10018];
8.获取CGRect,x,y,width,height
//For example:
CGRect frame = self.view.frame;
CGFloat x = CGRectGetMinX(frame);
CGFloat y = CGRectGetMinY(frame);
CGFloat width = CGRectGetWidth(frame);
CGFloat height = CGRectGetHeight(frame);
//Not:
CGRect frame = self.view.frame;
CGFloat x = frame.origin.x;
CGFloat y = frame.origin.y;
CGFloat width = frame.size.width;
CGFloat height = frame.size.height;
10.静态常量
//For example:
static NSString * const NYTAboutViewControllerCompanyName = @"The New York Times Company";
static const CGFloat NYTImageThumbnailHeight = 50.0;
//Not:
#define CompanyName @"The New York Times Company"
#define thumbnailHeight 2
11.Enumerated Types类型
//Example:
typedef NS_ENUM(NSInteger, NYTAdRequestState) {
NYTAdRequestStateInactive,
NYTAdRequestStateLoading
};
12.sharedInstance单利
+ (instancetype)sharedInstance {
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[[self class] alloc] init];
});
return sharedInstance;
}
13.Imports导入头文件,分模块
// Frameworks
@import QuartzCore;
// Models
#import "NYTUser.h"
// Views
#import "NYTButton.h"
#import "NYTUserView.h"
14.Protocols,定义协议方法
//For example:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
//Not:
- (void)didSelectTableRowAtIndexPath:(NSIndexPath *)indexPath;
2.在写代码之前确定应用的架构
好架构的优势:
1.任务均衡分摊给具有清晰角色的实体
2.可测试性通常都来自与上一条(对于一个合适的架构是非常容易)
3.易用性和低成本维护
体会:代码写的越少,Bug就越少
MVC,MVP,MVVM介绍
1.Models--负责主要的数据或者操作数据的 数据访问层
2.Views--负责展示层(GUI),对于iOS环境可以联想一下以 UI 开头的所有类。
3.Controller/Presenter/ViewModel--负责协调 Model 和 View,通常根据用户在View上的动作在Model上作出对应的更改,同时将更改的信息返回到View上。