OC
//無參block
@property(nonatomic,strong) void (^clickBlock0)(void);
//有參block
@property(nonatomic,strong) void (^clickBlock1)(NSString*text, NSString*text2);
//帶block的方法
- (void)requestSuccess:(void (^)(NSString*successText))success error:(void (^)(NSString*errorText, NSString*errorText2))error{
}
swift
//無參block
var clickBlock0: (() -> Void)?
//有參block
var clickBlock1: ((String, String) -> Void)?//定義
self.clickBlock1?("a","b")//賦值
self.clickBlock1 = { (param1: String, param2: String) in
//調(diào)用
print("接收到的參數(shù)是: \(param1) 和 \(param2)")
}
//帶block的方法
func performRequest(success: @escaping (String) -> Void, error: @escaping (String, String) -> Void) {
}
//賦值
func performRequest(success: @escaping (String) -> Void, error: @escaping (String, String) -> Void) {
let a = arc4random_uniform(2)
print(a)
if (a == 0){
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2)) {
success("1111")
}
} else {
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2)) {
error("222","3333")
}
}
}
//調(diào)用
performRequest(success: { successText in
print("Success: \(successText)")
}, error: { error, errorText in
print("Error: \(error), Error Text: \(errorText)")
})