1.為工程設(shè)置編碼風格規(guī)范
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.類屬性創(chuàng)建
//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.靜態(tài)常量
//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,定義協(xié)議方法
//For example:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
//Not:
- (void)didSelectTableRowAtIndexPath:(NSIndexPath *)indexPath;
2.在寫代碼之前確定應(yīng)用的架構(gòu)
好架構(gòu)的優(yōu)勢:
1.任務(wù)均衡分攤給具有清晰角色的實體
2.可測試性通常都來自與上一條(對于一個合適的架構(gòu)是非常容易)
3.易用性和低成本維護
體會:代碼寫的越少璧尸,Bug就越少
MVC,MVP,MVVM介紹
1.Models--負責主要的數(shù)據(jù)或者操作數(shù)據(jù)的 數(shù)據(jù)訪問層
2.Views--負責展示層(GUI)咒林,對于iOS環(huán)境可以聯(lián)想一下以 UI 開頭的所有類。
3.Controller/Presenter/ViewModel--負責協(xié)調(diào) Model 和 View爷光,通常根據(jù)用戶在View上的動作在Model上作出對應(yīng)的更改垫竞,同時將更改的信息返回到View上。