Block 是 Objective-C 對匿名函式的支援,可以視為「內嵌在程式碼中的一段可傳遞的邏輯」。它能宣告為變數、當作參數傳遞,也能直接在呼叫處內聯撰寫。本文用一個 Foundation 命令列專案,把 Block 的幾種常見形式走一遍。
這次不用 UIKit,直接開一個 Command Line Tool 來示範,好讓焦點留在語法本身。
步驟一,新增專案:

步驟二,Type 選擇 Foundation:

步驟三,新增一個類別,把要示範的方法都放進去:

#import <Foundation/Foundation.h> @interface TestBlockObject : NSObject // 一般實例方法 - (NSInteger)subtract:(NSInteger)paramValue from:(NSInteger)paramFrom; // 呼叫 Block - (void)callIntToString; - (void)doTheConversion; - (void)doTheConversionExt; - (void)doTheConversionExt2; @end
#import "TestBlockObject.h"
@implementation TestBlockObject
- (NSInteger)subtract:(NSInteger)paramValue from:(NSInteger)paramFrom
{
return paramFrom - paramValue;
}
// 寫法一:把 Block 指派給變數,如同函式指標
NSString* (^intToString)(NSUInteger) = ^(NSUInteger paramInteger) {
NSString *result = [NSString stringWithFormat:@"%lu", (unsigned long)paramInteger];
return result;
};
- (void)callIntToString
{
NSString *string = intToString(10);
NSLog(@"string = %@", string);
}
// 用 typedef 替 Block 取名字,讓簽名能在多處重用。
// 這行告訴編譯器:IntToStringConverter 是一個「接收 NSUInteger、回傳 NSString」的 Block
typedef NSString* (^IntToStringConverter)(NSUInteger paramInteger);
- (NSString *)convertIntToString:(NSUInteger)paramInteger
usingBlockObject:(IntToStringConverter)paramBlockObject
{
return paramBlockObject(paramInteger);
}
// 寫法二:把具名的 Block 變數當參數傳進去
- (void)doTheConversion
{
NSString *result = [self convertIntToString:123 usingBlockObject:intToString];
NSLog(@"result = %@", result);
}
// 寫法三:先指派給 typedef 型別的區域變數再傳入
- (void)doTheConversionExt
{
IntToStringConverter inlineConverter = ^(NSUInteger paramInteger) {
NSString *result = [NSString stringWithFormat:@"%lu", (unsigned long)paramInteger];
return result;
};
NSString *result = [self convertIntToString:123 usingBlockObject:inlineConverter];
NSLog(@"result = %@", result);
}
// 寫法四:直接在呼叫處內聯撰寫
- (void)doTheConversionExt2
{
NSString *result = [self convertIntToString:123 usingBlockObject:^NSString *(NSUInteger paramInteger) {
NSString *result = [NSString stringWithFormat:@"%lu", (unsigned long)paramInteger];
return result;
}];
NSLog(@"result = %@", result);
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
TestBlockObject *test = [[TestBlockObject alloc] init];
NSInteger i = [test subtract:10 from:20];
NSLog(@"Simple method result is ====> %ld", (long)i);
NSLog(@"callingBlockObject");
[test callIntToString];
NSLog(@"usingBlockObject");
[test doTheConversion];
NSLog(@"doTheConversionExt ====>");
[test doTheConversionExt];
NSLog(@"doTheConversionExt2 ====>");
[test doTheConversionExt2];
[test release];
// 最精簡的 Block:接收兩個參數、回傳相減結果
NSInteger (^subtract)(NSInteger, NSInteger) = ^(NSInteger paramValue, NSInteger paramFrom) {
return paramFrom - paramValue;
};
NSLog(@"Simple Block Object result is %ld", (long)subtract(10, 20));
}
return 0;
}
2013-05-09 17:35:45.335 TestBlockObject[1986:303] Simple method [test subtract:10 from:20] result is ====>10 2013-05-09 17:35:45.337 TestBlockObject[1986:303] callingBlockObject 2013-05-09 17:35:45.338 TestBlockObject[1986:303] string = 10 2013-05-09 17:35:45.339 TestBlockObject[1986:303] usingBlockObject 2013-05-09 17:35:45.339 TestBlockObject[1986:303] result = 123 2013-05-09 17:35:45.340 TestBlockObject[1986:303] doTheConversionExt ====> 2013-05-09 17:35:45.340 TestBlockObject[1986:303] result = 123 2013-05-09 17:35:45.341 TestBlockObject[1986:303] doTheConversionExt2 ====> 2013-05-09 17:35:45.341 TestBlockObject[1986:303] result = 123 2013-05-09 17:35:45.342 TestBlockObject[1986:303] Simple Block Object result is 10
實際開發時的取捨大致如下:
· 只有一兩行程式、且不需重用,直接內聯最省事,程式碼視覺上也最連貫。
· 同一段邏輯要在多處使用,指派給變數或 typedef 型別,避免重複。
· 要把 Block 當作 API 的參數(例如 completion handler),用 typedef 定義簽名,呼叫端一眼就看懂該傳什麼。
最後提醒一個常見陷阱:Block 會強引用它捕獲的物件變數。若在類別的方法裡宣告一個 Block 並在內部使用 self,而該類別又強引用這個 Block,就會形成循環引用。實務上的解法是在 Block 內使用 __weak typeof(self) weakSelf = self;,需要時再 strongify 回來。