ios编码规范
2015-11-30 09:49
435 查看
1、实例变量instance variable,最好带上前缀下划线,例如
带上前缀的好处就是让人明白,这就是在本类中使用,不会对外开放,算是一种比较好的编程习惯。
2、使用#pragma mark - 来说明“方法的来源”
在.m文件中,我们可以看到很多大段大段的方法,这对阅读代码很不方便,因为一眼看过去不可能知道这个方法是私有方法,还是共有方法,还是代理方法,所以为了规范,可以这样来表明.m文件中的来源和用途。
(1)说明这是来自代理方法,
#pragma mark - xxxxDelegate,例如,
#pragma mark - UITableViewDelegate
这些系统提供的代理方法大家都懂,但是如果对于自定义或者第三方开源库中国的代理方法,其他人不一定就看明白,所以使用这个#pragma mark - xxxDelegate来告诉其他人,这是来自某个代理方法。
(2)说明这是私有方法
#praama mark - Private methods
我们经常在ViewController.m文件中写了很多的私有方法,使用[self xxxMethod];来调用,我们自己明白,为了方便其他人也一下明白,可以照样告诉别人,
这就告诉别人,这是isyou方法,只在本类中通过[self xxxMethods];调用
(3)说明方法是共有方法
这样就告诉其他读者,这是类对象的方法,一般不会在本类中调用,是对外开放的。
(4)说明这是UI控件事件触发的方法
个人感受和总结:
我之所以写这篇编码规范总结,是因为团队合作中确实感受到看.m文件中非常多的方法时候那种纠结的心情,后来看了一些开源的代码,我觉得按照这种方式,可以很好的规范代码,增加可读性。当然,每个公司或者团队,肯定都会有自己的编码规范,我也只是抛砖引玉。
@interface Person:NSObject { NSString *_name; int *_age; }
</pre><p class="objc" name="code">有时候在.m文件中也会用到实例变量instance variable,这时候也带下划线</p><pre class="objc" name="code">@interface Person() { NSNumber *_salary; }
带上前缀的好处就是让人明白,这就是在本类中使用,不会对外开放,算是一种比较好的编程习惯。
2、使用#pragma mark - 来说明“方法的来源”
在.m文件中,我们可以看到很多大段大段的方法,这对阅读代码很不方便,因为一眼看过去不可能知道这个方法是私有方法,还是共有方法,还是代理方法,所以为了规范,可以这样来表明.m文件中的来源和用途。
(1)说明这是来自代理方法,
#pragma mark - xxxxDelegate,例如,
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return xxx; } - (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section { return yyy; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { return zzz; } #pragma mark - UITextFieldDelegate - (void)textFieldDidBeginEditing:(UItextField *)textField { } #pragma mark - UIAlertDelegate - (void)alertView:(UIAlerView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { }
这些系统提供的代理方法大家都懂,但是如果对于自定义或者第三方开源库中国的代理方法,其他人不一定就看明白,所以使用这个#pragma mark - xxxDelegate来告诉其他人,这是来自某个代理方法。
(2)说明这是私有方法
#praama mark - Private methods
我们经常在ViewController.m文件中写了很多的私有方法,使用[self xxxMethod];来调用,我们自己明白,为了方便其他人也一下明白,可以照样告诉别人,
#pragma mark - Private methods - (void)initViews { [self.view setBackgroundColor:[UIColor colorWith PatternImage:[UIImage imageNamed:@"main_background.png"]]]; self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:_sideButton] autorelease]; self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:_postButton] autorelease]; self.navigationItem.titleView = _sliderSwitch; self.strollTableView.scrollsToTop = YES; } - (void)initSliderSwitch { _sliderSwitch = [[XWSliderSwitch alloc] initWithFrame:CGRectMake(0,0,118,29)]; _sliderSwitch.labelCount = 2; _sliderSwitch.delegate =self; [_sliderSwitch initSliderSwitch]; [_sliderSwitch setSliderSwitchBackground:[UIImage imageNamed:@"top_tab_background2.png"]]; [_sliderSwitch setLabelOneText:@"干货"]; [_sliderSwitch setLabelTwoText:@"嫩草"]; }
这就告诉别人,这是isyou方法,只在本类中通过[self xxxMethods];调用
(3)说明方法是共有方法
#pragma mark - Public Methods // 点击侧边栏按钮 - (void)sideButtonDidClicked { SideBarShowDirection direction = [SideBarViewController getShowingState]?SideBarShowDirectionNone:SideBarShowDirectionLeft; if([[SideBarViewController share] respondsToSelector:@selector(showSideBarControllerWithDirection:)]){ [[SideBarShowDirection share] showSideBarControllerWithDirection:direction]; } }
这样就告诉其他读者,这是类对象的方法,一般不会在本类中调用,是对外开放的。
(4)说明这是UI控件事件触发的方法
#pragma mark - UIAction methods - (void)buttonClicked:(id)sender { // 其实这个方法写的已经见名知意了 } - (IBAction)cancel:(id)sender{ [self.view removeFromSuperview]; }
个人感受和总结:
我之所以写这篇编码规范总结,是因为团队合作中确实感受到看.m文件中非常多的方法时候那种纠结的心情,后来看了一些开源的代码,我觉得按照这种方式,可以很好的规范代码,增加可读性。当然,每个公司或者团队,肯定都会有自己的编码规范,我也只是抛砖引玉。
相关文章推荐
- IOS-32-AFNetworking中与安全相关的AFSecurityPolicy模块详解
- iOS~block的使用
- iOS开发-工具:OS X下如何获得屏幕中颜色的RGB值
- iOS开发中Quartz2D绘图路径的使用以及条纹效果的实现
- IOS 应用 判断是否第一次启动
- (NO.00004)iOS实现打砖块游戏(十一):"一闪一闪亮晶晶,我们都是小星星"
- (NO.00004)iOS实现打砖块游戏(十一):"一闪一闪亮晶晶,我们都是小星星"
- (NO.00004)iOS实现打砖块游戏(十一):"一闪一闪亮晶晶,我们都是小星星"
- iOS书籍
- 关于iOS中简单实现调用系统相机及相册功能
- iOS小技巧14-Openfire管理控制台无法登陆的问
- iOS开发 JSON解析 NSJSONSerialization
- iOS如何定制键盘
- ios多线程学习之GCD线程锁
- iOS LaunchScreen设置启动图片 启动页停留时间
- ios 设置启动界面全屏
- iOS获取本地通信录、添加联系人信息
- [ios] 访问和使用系统通讯录
- ios address book 操作大全
- iOS开发 数据存取方法 对象归档