浅谈OC对象初始化的三种姿势

本文通过三种不同类型的程序员——普通程序猿、文艺程序猿和二逼程序猿,展示了他们在编写相同功能代码时的不同风格。从简单的直接创建UIView,到使用Builder模式提高代码的可读性和整洁度,再到运用复杂的表达式提升代码效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、普通程序猿
普通程序员使用最常见路人姿势等场。普普通通,纯属陆仁辈。

陆仁贾写法:

// view 1
 
UIView *v1 = [UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
v1.backgroundColor = [UIColor whiteColor];
 
// view 2
 
UIView *v2 = [UIView alloc] initWithFrame:CGRectMake(0, 120, 200, 100)];
v2.backgroundColor = [UIColor whiteColor];
 
// add to view
[self.view addSubview:v1];
[self.view addSubview:v2];

 

 

撸人已写法:撸人已明显比陆仁贾聪明多了。使用大括号隔离,view1与view2相互独立,创建代码变量不会相互污染。

// view 1
{
    UIView *v1 = nil;
    UIView *v = [UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
    v.backgroundColor = [UIColor whiteColor];

    v1 = v;

    [self.view addSubview:v1];
}

// view 2
{
    UIView *v2 = nil;
    UIView *v = [UIView alloc] initWithFrame:CGRectMake(0, 120, 200, 100)];
    v.backgroundColor = [UIColor whiteColor];

    v2 = v;

    [self.view addSubview:v2];
}

 

路人饼写法

// view 1
UIView *v1 = nil;
{
    UIView *v = [UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
    v.backgroundColor = [UIColor whiteColor];
    v1 = v;
}

// view 2
UIView *v2 = nil;
{
    UIView *v = [UIView alloc] initWithFrame:CGRectMake(0, 120, 200, 100)];
    v.backgroundColor = [UIColor whiteColor];
    v2 = v;
}

[self.view addSubview:v1];
[self.view addSubview:v2];

 

二、文艺程序猿
文艺程序猿,使用教科书姿势登场。使用builder模式。使用block隔离初始化代码。

首先给NSObject增加扩展接口

// 扩展NSObject,增加Builder接口

@interface NSObject (Builder)
+ (id)z0_builder:(void(^)(id that))block;
- (id)z0_builder:(void(^)(id that))block;
@end

// 实现
@implementation NSObject (Builder)

+ (id)z0_builder:(void(^)(id))block {
    id instance = [[self alloc] init];
    block(instance);
    return instance;
}

- (id)z0_builder:(void(^)(id))block {
    block(self);
    return self;
}

@end

 

使用。代码简洁工整。处处都是文艺范。

- (void) foo {
// 使用
// view 1
UIView *v1 = [UIView z0_builder:^(UIView *that) {
    that.frame = CGRectMake(0, 0, 320, 200);
    that.background = [UIColor whiteColor];
}];

// view 2
UIView *v2 = [[UIView alloc] init];
[v2 z0_builder:^(UIView *that) {
    that.frame = CGRectMake(0, 0, 320, 200);
    that.background = [UIColor whiteColor];
}];

// 添加到父视图
[self.view addSubview:v1];
[self.view addSubview:v2];
}

 

三、二逼程序猿
最后入场的是二逼程序猿。

!#!@#@%&……&%&#¥%!@#¥!@#¥!!!!! 这个是什么卵?
其实....我也不知道!>_<# 自行领悟。
黑科技?????呵呵~~ 我就是代码少,你吹啊~~

- (void) foo {
// view 1
  UIView *v1 = ({
      UIView *v = [UIView alloc] init];
      v.frame = CGRectMake(0, 0, 320, 200);
      v.background = [UIColor whiteColor];
      v;
  });

  // view2
  UIView *v2 = ({
      UIView *v = [UIView alloc] init];
      v.frame = CGRectMake(0, 120, 320, 200);
      v.background = [UIColor blueColor];
      v;
  });

  [self.view addSubview:v1];
  [self.view addSubview:v2];
}

转载于:https://www.cnblogs.com/weiboyuan/p/5955126.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值