一:内嵌函数引用inout的参数时不能是逃逸的
func func1(inoutPara para : inout Int) {
para = 3
func nestFun() -> () -> Int{
return {
return para + 1
}
}
nestFun()
}
编译器会报错:Escaping closure captures 'inout' parameter 'para'
二:如果不对inout参数进行修改,可以采用这种形式解决:
func nestFun() -> () -> Int{
para = 4
return {
[para] in
return para + 1
}
}
如果对inout参数进行修改,可以采用复制inout参数修改后再赋值给inout参数这种形式解决:
func func2(queue:DispatchQueue,inoutPara para :inoutInt) {
var localX = para
defer{ para = localX }
// Operate on localX asynchronously, then wait before returning.
queue.async { someMutatingOperation(&localX) }
queue.sync{}
}
详细文档 Memory Safety.
三:func中不可再操作inout的原变量
var para=5
func inoutfunc(inoutpara :inoutInt) {
print(inoutpara)
print(para)
}
inoutfunc(inoutpara: ¶)
报错:error: Execution was interrupted,reason: signal SIGABRT.
The process has been left at the point where it was interrupted,use "thread return -x" to return to the state before expression evaluation.
上例中,不可使用print(para)语句