在iOS開發中,可以通過以下幾種方式實現多參數傳遞:
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) {
// 處理參數
}];
無論采用哪種方式,多參數的傳遞都需要在方法定義和調用的地方進行一致的處理。