实现多参数传递

// MARK: - 写入混合数据 // This method takes a nil-terminated list of objects.

- (void)writeMixtureDataTodirectoryPath:(NSString *)directoryPath   WithData:(NSString *)firstObj, ...NS_REQUIRES_NIL_TERMINATION;


- (void)writeMixtureDataTodirectoryPath:(NSString *)directoryPath WithData:(NSString *)firstObj, ...NS_REQUIRES_NIL_TERMINATION{
NSString *eachObject;
//1.定义一个指向个数可变的参数列表指针;
// 用于存放取出的参数
va_list args;
// 初始化变量刚定义的va_list变量,这个宏的第二个参数是第一个可变参数的前一个参数,是一个固定的参数
va_start(args, firstObj);
if (firstObj){
    while (YES)
    {
        NSString * otherString = va_arg(args, NSString *);
        NSLog(@"otherString %@",otherString);
        if (!otherString) {
            break;
        }
    }
}
//结束可变参数的获取  
va_end(args);
}

for example:

+ (void)createAlertWithTitle:(NSString *)title message:(NSString *)message tagert:(UIViewController *)tagert cancelButtonTitle:(NSString *)cancelButtonTitle complementBlock:(void(^)(NSInteger clickIndex))complementBlock otherButtonTitles:(NSString *)firstbtnTitle, ...NS_REQUIRES_NIL_TERMINATION{

NSInteger clickindex = 0;
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
NSLog(@"传多个参数的第一个参数 %@",firstbtnTitle);
if(cancelButtonTitle != nil && ![cancelButtonTitle isEqualToString:@""]){
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        if(complementBlock){
            complementBlock(-1);
        }
    }];
    [alert addAction:cancleAction];
}

UIAlertAction *tempAction = [UIAlertAction actionWithTitle:firstbtnTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    if(complementBlock){
        complementBlock(clickindex);
    }
}];
[alert addAction:tempAction];
//1.定义一个指向个数可变的参数列表指针;
// 用于存放取出的参数
va_list args;
// 初始化变量刚定义的va_list变量,这个宏的第二个参数是第一个可变参数的前一个参数,是一个固定的参数
va_start(args, firstbtnTitle);
if (firstbtnTitle){
    while (YES)
    {
        NSString * otherString = va_arg(args, NSString *);
        if(otherString){
            clickindex += 1;
            UIAlertAction *tempAction = [UIAlertAction actionWithTitle:otherString style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if(complementBlock){
                    complementBlock(clickindex);
                }
                
            }];
            [alert addAction:tempAction];

        }
        NSLog(@"otherString %@",otherString);
        if (!otherString) {
            break;
        }
    }
}
//结束可变参数的获取
va_end(args);
[tagert presentViewController:alert animated:true completion:nil];

}

你可能感兴趣的:(实现多参数传递)