您的位置:首页 > 移动开发 > IOS开发

ios编码规范

2015-11-30 09:49 435 查看
1、实例变量instance variable,最好带上前缀下划线,例如
@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文件中非常多的方法时候那种纠结的心情,后来看了一些开源的代码,我觉得按照这种方式,可以很好的规范代码,增加可读性。当然,每个公司或者团队,肯定都会有自己的编码规范,我也只是抛砖引玉。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: