swift函数参数指针传递inout和@escaping冲突的解决方法

54 篇文章 1 订阅
47 篇文章 1 订阅


今天深入解决关于函数参数指针的问题。问题是这样,我在swift的一个函数里面,参数里需要传递一个指针类型的Int,但是这个函数里面还有一个逃逸闭包@escaping,于是就会报错这样:Escaping closure captures ‘inout’ parameter ‘selectRow’ ,意思是说:逃逸闭包补货了一个inout参数 selectRow

需求

我每次调用下面的swift方法,都会弹出一个PickerView,并且,设置label的text,同时,记录上次选中的selectRow

 func setTimePicker2(selectRow:inout Int,label:UILabel){
        let vc = TDWPickerVC(items: timeItems, titleLabelStr: "请选择时间段",selectRow: selectRow) {
            print("点击取消")
        } confirmBlock: { (str,row) in
            print("点击确定",str)
            selectRow = row
            label.text = str
        }
        print("time1VSelectRow=",selectRow )
        vc.modalPresentationStyle = .custom
        vc.modalTransitionStyle = .crossDissolve
        navigationController?.present(vc, animated: true, completion: nil)
    }

下面是调用的代码

var time3VSelectRow = 0//类成员变量
setTimePicker2(selectRow: &time3VSelectRow, label: time3Label)//方法中的调用
``
再看控制器的定义里面有一个@escaping的逃逸闭包类型
```swift
  init(items:[String],titleLabelStr:String,selectRow:Int, cancleBlock:@escaping BlockVoid,confirmBlock:@escaping BlockStr) {
        self.titleLabelStr = titleLabelStr
        self.cancleBlock = cancleBlock
        self.confirmBlock = confirmBlock
        self.items = items
        self.selectRow = selectRow
        super.init(nibName: "TDWPickerVC", bundle: nil)
        
    }

错误原因就是编译器不允许逃逸闭包@escaping捕获inout类型参数,因为inout类型传入的是一个指针,参数可以在外面更改,不安全

如果上面代码没有逃逸闭包,那么传入一个inout Int 类型就没问题了。setTimePicker2方法在执行的时候同时更改selectRow这个Int数据

解决方法

创建一个类,用类包裹基础数据类型Int这个类只用来存选中行数,这个类只用来存选中行数,因为直接用基础数据类型inout int,不能在里面再调用逃逸闭包@escaping,但是参数传入类的对象,是直接操作对象里面的成员,操作的地址相同。

class TimeVSelected:NSObject{
    var selectRow : Int = 0
}

函数定义成这样,参数列表里面要类对象TimeVSelected,原来Int类型的地方用 selectobj.selectRow 对象的成员变量来代替

    func setTimePicker(selectobj:  TimeVSelected,label:UILabel){
    	//注意这里用selectobj.selectRow代替之前的selectRow
        let vc = TDWPickerVC(items: timeItems, titleLabelStr: "请选择时间段",selectRow: selectobj.selectRow) {
            print("点击取消")
        } confirmBlock: { (str,row) in
            print("点击确定",str)
            selectobj.selectRow = row
            print("selectobj地址是:",selectobj)
            label.text = str
        }
        vc.modalPresentationStyle = .custom
        vc.modalTransitionStyle = .crossDissolve
        navigationController?.present(vc, animated: true, completion: nil)
    }

容易产生的问题

之前我也试过,既然类对象可以直接修改里面的对象,inout 和 @escaping不能一起用,那我为什么不能直接去掉inout这样设置参数列表:

 func setTimePicker2(selectRow:Int,label:UILabel) 

这样会报错,因为函数参数是不能在函数体里面改变的,函数参数被认为是let常量,如下:报错

func setTimePicker2(selectRow:Int,label:UILabel){
       let vc = TDWPickerVC(items: timeItems, titleLabelStr: "请选择时间段",selectRow: selectRow) {
           print("点击取消")
       } confirmBlock: { (str,row) in
           print("点击确定",str)
           selectRow = row //这里会报错Cannot assign to value: 'selectRow' is a 'let' constant
           label.text = str
       }
       print("time1VSelectRow=",selectRow )
       vc.modalPresentationStyle = .custom
       vc.modalTransitionStyle = .crossDissolve
       navigationController?.present(vc, animated: true, completion: nil)
   }

xcode截图如下:
在这里插入图片描述
我们用类对象在函数体也不能改变,但是对象成员可以改变,看我下面的截图,报错也是说对象是let类型不能改:

 func setTimePicker(selectobj:  TimeVSelected,label:UILabel){
        let vc = TDWPickerVC(items: timeItems, titleLabelStr: "请选择时间段",selectRow: selectobj.selectRow) {
            print("点击取消")
        } confirmBlock: { (str,row) in
            print("点击确定",str)
            selectobj.selectRow = row
            selectobj = TimeVSelected()//这里就会报错,因为selectobj是函数参数,不能赋值
            print("selectobj地址是:",selectobj)
            label.text = str
        }
        vc.modalPresentationStyle = .custom
        vc.modalTransitionStyle = .crossDissolve
        navigationController?.present(vc, animated: true, completion: nil)
    }

在这里插入图片描述
以前用C++的时候这种函数参数列表里的参数叫形参,是可以改的,但是改完以后,不会影响函数传入之前的值,形参是临时变量,函数结束就释放了,swift就不一样,类对象作为函数参数,实际就是把类对象地址传过来,跟c++一样,改变对象里成员值,对象也被改变

print("timeVselected1地址是:",timeVselected1)

通过打印证明函数参数的类对象传递和C++的地址传递一样,函数外和内部都是一个地址
在这里插入图片描述

总结

以后再需要传递那种在函数里改变函数外的值,恰好函数内部有逃逸闭包@escaping的,就用类对象传递到函数中,再改变类对象的成员,这样就能在函数内改变函数外的值了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值