在iOS開發(fā)中,可以通過以下幾種方式實(shí)現(xiàn)多參數(shù)傳遞:
NSDictionary *params = @{@"param1": @"value1",
@"param2": @"value2",
@"param3": @"value3"};
[self doSomethingWithParams:params];
@interface MyParams : NSObject
@property (nonatomic, strong) NSString *param1;
@property (nonatomic, strong) NSString *param2;
@property (nonatomic, strong) NSString *param3;
@end
MyParams *params = [[MyParams alloc] init];
params.param1 = @"value1";
params.param2 = @"value2";
params.param3 = @"value3";
[self doSomethingWithParams:params];
typedef void (^MyCompletionBlock)(NSString *param1, NSString *param2, NSString *param3);
- (void)doSomethingWithCompletion:(MyCompletionBlock)completion {
NSString *param1 = @"value1";
NSString *param2 = @"value2";
NSString *param3 = @"value3";
completion(param1, param2, param3);
}
[self doSomethingWithCompletion:^(NSString *param1, NSString *param2, NSString *param3) {
// 處理參數(shù)
}];
無論采用哪種方式,多參數(shù)的傳遞都需要在方法定義和調(diào)用的地方進(jìn)行一致的處理。