温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

iPhone开发重构:提取公用的方法以清理重复代码

发布时间:2020-04-23 13:50:25 来源:网络 阅读:1201 作者:benjielin 栏目:开发技术

     无论在iPhone开发还是学习的过程中都会看到一些不是很理想的代码,不可否认自己也在不断“贡献”着这类代码。面对一些代码的“坏味道”,重构显然是个有效的解决途径。《iPhone开发重构》系列就想总结和补充iPhone开发中经历的一些重构,其间可能会引用一些开源以及实际项目的代码,本着对技术的探求,冒昧之处还请作者多多见谅。

 

   代码重复是一个比较明显的“坏味道”,提取公用的方法就是解决的途径之一。iPhone开发中,使用UITableView的时候就有如下一段“经典”的模板代码,因为这是项目模板自动生成的,所以很多人就自然接受了。但随着越来越多地通过copy&paste在一个项目中使用这段代码,大家是否有些采取行动的压力呢?好吧,我们就从这“动刀”吧!

 

重构前:

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"wikiHowCell";
    UITableViewCell *cell = (WHTableViewCell *)[tv dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[WHTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.title = [[featuredArticles objectAtIndex:indexPath.row] objectForKey:@"title"];
    return cell;
}

 

   对此我们可以提取个公共的方法,并放置在一个适当的地方。UITableViewCell的Category应该是一个比较好的去处。重构后提取的方法以及实际调用的代码如下:

重构后:

@implementation UITableViewCell(Cache)

+ (id)dequeOrCreateInTable:(UITableView*)tableView withId: (NSString*)reuseId andStyle:(UITableViewCellStyle)style {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:style reuseIdentifier:reuseId] autorelease];
    }
    return cell;
}

@end

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [UITableViewCell dequeOrCreateInTable:tableView withId:@"wikiHowCell" andStyle:UITableViewCellStyleDefault];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.title = [[featuredArticles objectAtIndex:indexPath.row] objectForKey:@"title"];
    return cell;
}

   从代码量衡量,仅从此处可能感觉重构前后变化不大,甚至还会略有增多。但如果考虑到公用方法的多次使用产生的“效益”,付出的努力应该是值当的!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI