定義一個函數類型的變量,并賦值為函數。系統就報錯
//將函數作為一種數據類型 func?changeScore(inout?scores:Array<Int>,operater?op:(inout?Array<Int>)->()){ ????op(&scores); } func?allAddScore(inout?scores:Array<Int>)->(){ ????for?i?in?0..<scores.count{ ????????scores[i]?+=?5; ????} } func?allTransfer(inout?scores:Array<Int>){ ????for?i?in?0..<scores.count{ ????????scores[i]=Int(Double(scores[i])/150.0*100.0); ????} } func?allSqrt(inout?scores:Array<Int>){ ????for?i?in?0..<scores.count{ ????????scores[i]=Int(sqrt(Double(scores[i]))*10.0); ????} } var?scores:[Int]=[60,30,55,90,100,78,94]; allAddScore(&scores); scores; scores=[150,120,100,90,72]; allTransfer(&scores); scores; scores=[100,60,50,36,80,75,65]; allSqrt(&scores); scores;
目前代碼運行到這里,單獨調用每個函數都沒問題。結果都是對的。
scores=[1,2,3,4,5,6]; //var?myDelegate?=?allAddScore; //changeScore(&scores,?operater:?myDelegate); changeScore(&scores,?operater:?allAddScore); scores;
代碼運行到這里,如果我在調用changeScore這個函數時直接傳入的是allAddScore這個函數名,那么運行結果也沒有問題。
但是,如果我單獨定義一個變量myDelegate將其賦值為函數名allAddScore,這時候編譯器不會出現紅色感嘆號,表示代碼是沒有問題的。但是XCode就報錯了。錯誤說明是com.apple.dt.xcode.Playground這個service create a crash log.然后編輯區的右邊就沒有任何結果了。
各位大神,幫忙看看這個是什么原因?!
2016-07-14
這個代碼在我這里一切正常,沒發現有錯誤