如何像 Objective-C 一样在 Swift 中定义块?
How to define block in Swift like in Objective-C?
在 ObjectiveC 中,我可以定义将在很多地方使用的块,例如
typedef void (^APISuccessHandler)(RKObjectRequestOperation *operation, RKMappingResult *result);
然后在(例如)ViewController 属性
中使用它
@property (nonatomic, copy) APISuccessHandler successHandler;
如何在 swift 中做同样的事情?
使用typealias
typealias MyType=(str:String,num:Int)->()
然后,
var test:MyType = {(str,num) in
println(str)
println(num)
}
执行区块
test(str: "123", num: 1)
输出
123
1
在 ObjectiveC 中,我可以定义将在很多地方使用的块,例如
typedef void (^APISuccessHandler)(RKObjectRequestOperation *operation, RKMappingResult *result);
然后在(例如)ViewController 属性
中使用它@property (nonatomic, copy) APISuccessHandler successHandler;
如何在 swift 中做同样的事情?
使用typealias
typealias MyType=(str:String,num:Int)->()
然后,
var test:MyType = {(str,num) in
println(str)
println(num)
}
执行区块
test(str: "123", num: 1)
输出
123
1