一:內(nèi)嵌函數(shù)引用inout的參數(shù)時不能是逃逸的
func func1(inoutPara para : inout Int) {?? ?? ?
para = 3? ?
????func nestFun() ->? () -> Int{? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
????????return {?
????????????return para + 1
????????}? ?
????}? ?
????nestFun()?
}
編譯器會報錯:Escaping closure captures 'inout' parameter 'para'
二:如果不對inout參數(shù)進行修改援奢,可以采用這種形式解決:
?func nestFun() ->? () -> Int{? ? ? ? ? ??
????????para = 4? ? ? ? ? ? ? ? ? ? ?
????????return {
????????????[para] in
????????????return para + 1
????????} ? ?
????} ? ?
如果對inout參數(shù)進行修改台舱,可以采用復(fù)制inout參數(shù)修改后再賦值給inout參數(shù)這種形式解決:
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)語句