80_iOS干货43_调用OC方法传递多个参数的11种方法

一,方法中写多个参数调用

- (void)test1 {
    [self testFunctionWithParams1:6 :@"234131#"];
}
- (void)testFunctionWithParams1:(int )number :(NSString *)string {
    NSLog(@"方法多个参数:%i%@",number, string);
}

二,performSelector调用方法

#pragma mark -  2,performSelector传参数:最多两个参数
- (void)test2 {
    [self performSelector:@selector(testFunctionWithParams1::) withObject:@5 withObject:@"67"];
    
//    动态加载方法:需要对指针做处理,最多两个参数
//    SEL selector = NSSelectorFromString(@"testFunctionWithParams2::");
//    IMP imp = [self methodForSelector:selector];
//    void (*func)(id, SEL) = (void *)imp;
//    if([self respondsToSelector:selector]){
//        func(self,selector);
//    };
    
//    也可以采用延时,但只能传一个参数
//    SEL selector = NSSelectorFromString(@"testFunctionWithParams2:");
//    [self performSelector:selector withObject:@"tom" afterDelay:0.1f];
}
- (void)testFunctionWithParams:(NSString *)string {
     NSLog(@"performSelector传参数:%@", string);
}

- (void)testFunctionWithParams2:(int )number :(NSString *)string {
     NSLog(@"performSelector传参数:%i%@",number, string);
}

三,传字典,封装成对象

#pragma mark -  3,传字典
- (void)test3 {
    //封装参数
    NSDictionary *dic = @{@"param1":@"this is a string",@"param2":@[@2,@3,@3],@"param3":@123};
    //调用方法
    [self testFunctionWithParams3:dic];
    
}
- (void)testFunctionWithParams3:(NSDictionary *)paramDic {
    NSLog(@"传字典:%@", paramDic);
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值