您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發基礎

Objective-C 物件初始化的三種寫法與 Builder 模式

編輯:iOS 開發基礎

早上看到有人寫了篇《Swift:讓人眼前一亮的初始化方式》。什麼?初始化?Objective-C 也來聊聊吧。

一、路人寫法

最常見的直觀寫法,樸實無華。

// 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];

// 加入父視圖
[self.view addSubview:v1];
[self.view addSubview:v2];

稍高一點的版本:用大括號隔離作用域,讓 v1 與 v2 的建立程式碼彼此獨立,變數不會互相污染。

// 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];
}

三、GCC 延伸語法

最後這種寫法用到了編譯器的 statement expression 擴充:

乍看之下有點莫名其妙,但它其實是 GCC/Clang 的語法擴充,用來把一段陳述式當成表達式求值,最後一個表達式的值就是整體結果。

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

    // view 2
    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];
}

三種寫法各有取捨:括號隔離最簡單直觀,Builder 模式可讀性最好,statement expression 則最精簡但依賴編譯器擴充、可移植性較差。挑一個團隊都能讀懂的就好。

文章轉自 kim4apple 的簡書

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved