2017-10-10 59 views
0

如何在使用「_」的swift中爲函數添加和使用第二個參數?例如,在本功能:將第二個參數添加到函數

func save(completion: (_ finished: Bool) ->()) { 

當我使用它是這樣的:

self.save { (complete) in 
     if complete { 
      dismiss(animated: true, completion: nil) 
     } 
    } 

但我想添加另一種說法,一個的UIColor。每次我這樣做,我都會得到錯誤,因爲我沒有正確使用參數。如何在我的func中添加UIColor?我做了這樣的

func save(color: UIColor,completion: (_ finished: Bool) ->()) { 

,當我用:

self.save(color: green, { (complete) in 
    if complete { 
     dismiss(animated: true, completion: nil) 
    }) 
} 

,但我得到了布爾類型的錯誤

回答

2

你必須寫

self.save(color: green) { complete in // the parentheses are not needed 
    if complete { 
     dismiss(animated: true, completion: nil) 
    } 
} 
+0

這工作!非常感謝! – newbiejava1998

0

當你在完成時調用一個函數,調用函數時不會有錯誤的最簡單的方法,當你點擊時單擊輸入e在論據的「藍色方塊」上。它會編輯所有的功能,因爲它應該看起來像。