2017-08-08 22 views
0

這不是常見的情況,我想知道用戶是否通過了該值,或者它是否使用了Swift中的默認值。例如對於檢查用戶是否通過該值,或者它在Swift函數中使用了默認的可選參數

func test(firstThing: Int? = nil) { 
    if firstThing.isNil { 
     if // firstThing used the default value { 
      print("default") 
     } else { 
      print("nil ingresed") 
     } 
    } 
} 

它應該有以下行爲: 測試()// 「默認」 測試(firstThing:無)// 「無ingresed」

+0

您已經分配的默認值爲零,所以天氣的用戶傳遞價值爲零或編譯器使用您的默認值將總是得到默認值 – OverD

+0

@OverD有沒有辦法來檢查用戶是否通過該值? –

+0

看看這個線程:https://stackoverflow.com/questions/37305951/default-optional-parameter-in-swift-function – Tom

回答

0

爲此,你必須寫兩個函數一樣的名字。但是,在一個函數u盤周邊,而在另一個沒有周邊,例如: -

class ViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.myFunc() 

     self.myFunc("demo") 


     // Do any additional setup after loading the view, typically from a nib. 
    } 


    func myFunc() { 
     print("empty") 
    } 

    func myFunc(_ str : String){ 
     print(str) 
    } 

} 
相關問題