定義一個(gè)函數(shù)類型的變量,并賦值為函數(shù)。系統(tǒng)就報(bào)錯(cuò)
//將函數(shù)作為一種數(shù)據(jù)類型 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;
目前代碼運(yùn)行到這里,單獨(dú)調(diào)用每個(gè)函數(shù)都沒問題。結(jié)果都是對(duì)的。
scores=[1,2,3,4,5,6]; //var?myDelegate?=?allAddScore; //changeScore(&scores,?operater:?myDelegate); changeScore(&scores,?operater:?allAddScore); scores;
代碼運(yùn)行到這里,如果我在調(diào)用changeScore這個(gè)函數(shù)時(shí)直接傳入的是allAddScore這個(gè)函數(shù)名,那么運(yùn)行結(jié)果也沒有問題。
但是,如果我單獨(dú)定義一個(gè)變量myDelegate將其賦值為函數(shù)名allAddScore,這時(shí)候編譯器不會(huì)出現(xiàn)紅色感嘆號(hào),表示代碼是沒有問題的。但是XCode就報(bào)錯(cuò)了。錯(cuò)誤說明是com.apple.dt.xcode.Playground這個(gè)service create a crash log.然后編輯區(qū)的右邊就沒有任何結(jié)果了。
各位大神,幫忙看看這個(gè)是什么原因?!
2016-07-14
這個(gè)代碼在我這里一切正常,沒發(fā)現(xiàn)有錯(cuò)誤