Objective-C代码规范指导

Objective-C代码规范指导

2023年7月14日发(作者:)

Objective-C语言代码风格指导良好代码风格是一种艺术作者:woodjobber博客:简介代码规范目的不是提供死板的规定,仅仅是建议,提供给开发者之间降低阅读代码的障碍。优秀的代码应该具备:代码简洁简明代码简洁简明,代码自我翻译代码自我翻译等等特点。我一直认为--良好代码规范优先于性能。良好代码规范优先于性能编程规范点语法对于getter,setter属性尽量使用点(.)语法(除了在init和dealloc方法中,应该直接使用实例变量,至于原因稍后解释)。Preferred:oundColor = [UIColor redColor];[UIApplication sharedApplication].delegate;Not Preferred:[view setBackgroundColor:[UIColor redColor]];te;条件判断条件语句体应该总是被花括号{}包裹,包括仅仅只有一句代码,且都不能省略,if与左圆括弧(之间有一个空格,右圆括弧)与左花括号{之间有一个空格。多条件判断的时候,可以先判断否定的情况。Preferred:if (!isVaild) { return NO;}if (!isVaild) {return NO;}Not Preferred:if (!isVaild)

return NO;Orif (!isVaild) return NO;Preferred:- (BOOL)isValidUser:(User *)user { if (!) {return NO;} if (!rd) {return NO;} if (!) {return NO;} return YES;}Or- (BOOL)isValidUser:(User *)user { BOOL isValidForName = ; BOOL isValidForPassword = rd; BOOL isValidForEmail = ; BOOL isInValid = !isValidForName || !isValidForPassword || !isValidForEmail; if (isInValid) { return NO; } //其他条件 return YES;}Not Preferred:- (BOOL)isValidUser:(User *)user { BOOL isValid = NO; if () { if (rd)

{ if () { isValid = YES; } } } retrun isValid;}尤达表达式最好不要使用尤达表达式。尤达表达式是指,比较者与被比较者之间的前后关系,不要用比较者与被比较者相比较

而是

应该用被比较者与比较者相比较。简单说,就是用

变量与

参照物相比较,而不是参照物与变量相比较。Preferred:id value = ?;//?代表某一个对象if ([value isEqual:@12]) { //code}Not preferred:if (@12 isEqual:value) { //code}三元运算符当三元运算符的第二个参数的返回值与检查对象一致时,这样使用:Preferred:result = obj?:[self callOtherMethod];Not Preferred:result = obj? obj : [self callOtherMethod];字面值对于字面值我们应该使用NSString,NSDictionary,NSArray,NSNumber的简写形式来创建对象。Preferred:NSArray *cities = @[@"成都",@"北京",@"上海"];NSDictionary *productPrice = @{@"iPhone5":@"4300",@"iPhone6":@"4800"};NSNumber *age = @23;Not Preferred:NSArray *cities = [NSArray arrayWithObjects:@"成都",@"北京",@"上海",nil];NSDicitionary *productPrice = [NSDictionary dictionaryWithObjectsAndKeys:@"4300",@"iPhone5",@"4800",@"iPhone"];方法对于方法应该遵循苹果的规范,(-/+)应该与左圆括弧(有一个空格。Preferred:- (void)sendMessage;Not Preferred:-(void)sendMessage;变量Preferred:@interface Person: NSObject@property (nonatomic,copy) NSString *name;@endNot Preferred:@interface Person: NSObject { NSString *name;//或NSString *_name;}init

和 deallocdealloc方法应该放在实现文件的顶部,直接放在@sythesize

@dynamic的后面,init方法应该放在dealloc方法的后面。注意init方法里面永远不应该调用setter、getter方法,你应该直接访问实例变量。至于什么原因?在于一个对象只有在init返回之后,才完成初始化状态,只有这样才能说明创建的对象已经处于稳定的状态。同样,在dealloc中,也不能这样做。简单的一句话总结:总是使用y这种形式(出于对内存管理考虑),切记除了在

init和dealloc方法中,应该直接访问实例变量。原因:If you're trying to be as defensive as possible, then you have to operate under the assumption that someone may have subclassed the class andoverridden your public methods. These overrides may be assuming that when the method is invoked, all of the properties and ivars and everythingare in a consistent and fully-initialized , if your initializer or dealloc method causes one of these overridden methods to be executed, then the logic of the method may do somethingincorrect. The best case scenario is that it works as intended. The worst case scenario is that you do something like dereference a NULL pointer orindex beyond the bounds of an array or cause some data to become corrupted, etc.`While you're inside an initializer or dealloc method, your object is in an inconsistent state. Thus, if you're trying to be as defensive as possible, youshould avoid invoking methods that might be making assumptions about the state of the object.请参考Ivars should be directly accessed in init'ers #6Don’t Use Accessor Methods in Initializer Methods and deallocPereferred:@implement@sythesize age;@dynamic name;- (void)dealloc {}- (id)init { if (self = [super init]) { } return self;}@endPereferred:- (id)init { self = [super init]; if (self) { _name = @"jumei"; _age = @"4"; } return self;}Not Pereferred:- (id)init { if (self = [super init]) { = @"jumei"; = @"4"; } return self;}顺便提一句,不要在init方法中,初始化耗时的代码,init方法中应该尽可能快速完成初始化并返回。BOOL

和 nil变量一定不要直接与YES作比较,至于什么原因?请参考BOOL,你真的了解吗?Pereferred:if (!obj) {}if (obj == nil) {}if (!isValid) {}if (isvalid == NO) {}Not Pereferred:if (isValid == YES) {}instancetype & id在方便构造器中,一定要使用

intancetype,而且在init方法应该多使用instancetype。请参考为什么应该用intancetype替代idpereferred:- (instancetype)initWithName:(NSString)name { if (self = [super init]) { } return self;

}+ (instancetype)initWithName:(NSString)name { if (self = [super init]) { } return self;

}Block的循环引用当使用block时,一定要注意避免引用循环。可以使用@weakify/@strongify。对于只调用一个方法:Pereferred:__weak __typeof(self)weakSelf = self;[self executeBlock:^(NSString *str) { [weakSelf performSomethingWithString:str];}];Not Pereferred:[self executeBlock:^(NSString *str) { [self performSomethingWithString:str];}];对于调用多个方法:Pereferred:__weak __typeof(self)weakSelf = self;[self executeBlock:^(NSString *str) { __strong _typeof(weakSelf) strongSelf = weakSelf; if (strongSelf) { [strongSelf performSomethingWithString:str]; [strongSelf performOtherSomethingWithString:str]; }}];NOt Pereferred:__weak __typeof(self)weakSelf = self;[self executeBlock:^(NSString *str) { [weakSelf performSomethingWithString:str]; [weakSelf performOtherSomethingWithString:str];}];命名规范命名注意两点:可读性可读性与命名冲突命名冲突。类命名以大写字母作为前缀,可以使用公司缩写或者项目名称缩写。例如以WJUserEnity。形式:大写字母+类名;命名要清晰,能够描述该类的功能。常量命名常量命名应该以驼峰命名,并以相关的类名作为前缀。形式:类名+属性。Pereferred:static const CGFloat WJOrderListTableViewCellHeight = 44.0f;NSString *const WJOrderTableViewCellIdentifier = @"WJOrderTableViewCellIdentifier";变量命名变量命名遵守尽可能描述的清楚,但也不要画蛇添足。形式:修饰词+[类型]。Pereferred:NSString *title;NSAttributedString *titleAttributedString;NSDate *lastModifiedDate;Not Pereferred:NSString *titleString;NSAtttributedString *titleAttributedStr;NSDate *lastModified;#define命名以k开头+类名+属性的形式。Pereferred:#define kWJOrderListTableViewCellHeight = 22.0f;枚举命名枚举命名要加相关类名前缀并且枚举值要有枚举类型前缀,最好用NS_ENUMPereferred:typedef NS_ENUM(NSInteger,UIViewAnimationTransition) { UIViewAnimationTransitionNone, UIViewAnimationTransitonFlipFromLeft,}方法命名方法命名主体采用驼峰命名。方法名前不要有下划线(_),我比较推荐的一种写法是如果是私有方法,用字母+下划线的形式;可以使用情态动词(can,should,will等)来阐明含义,不要使用do。Pereferred:- (void)requestNetwork;- (void)shouldRequstNetwork;私有方法- (void)wj_requestNetwork;Not Pereferred:- (void)doRequstNetork;- (void)_requstNetwork;带多参数方法命名方法返回类型与(-/+)符合之间应该有一个空格。尽可能少用"and"这个词。遵循苹果风格,参数前应该有一个描述性关键词。pereferred:-(void)setProductName:(NSString *)name;- (void)sendMessage:(NSString *)message toClient:(NSString *)client;- (instancetype)initWithWidth:(CGFloat)width height:(CGFloat)height;Not Pereferred:-(void)setProductName:(NSString *)name;- (void)sendMessage:(NSString *)message :(NSString *)client;- (instancetype)initWithWidth:(CGFloat)width andHeight:(CGFloat)height;类目命名类目命名必须描述该类目的功能,且首字母必须大写。Pereferred:@interface UIViewController (JMAudioPlaying)Not Pereferred:@interface UIViewController (audioPlaying)类目方法命名类目方法命名的格式:小写字母开头+下划线+功能描述;Pereferred:- (id)jm_itemAtIndex:(NSUInteger)index;Not Pereferred:- (id)itemAtIndex:(NSUInteger)index;这种格式是苹果官方推荐的,请参考Avoid Category Method Name Clashes协议命名协议命名格式:类名+功能描述@protocol JMPickerViewDelegate @end@protocol JMMessageData @end代理方法命名协议方法命名形式:某类某个对象做了什么。Pereferred:@protocol JMPickerViewDelegate - (void)pickerView:(JMPickerView *)pickerView didTapRightButton:(UIButton *)button;@endNot Pereferred:@protocol JMPickerViewDelegate - (void)didTapRightButton:(UIButton *)button;@end

发布者:admin,转转请注明出处:http://www.yc00.com/web/1689265360a226463.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信