2017-09-19 27 views
-2

我有一個功能deductCoins獲取功能價值的功能範圍以外的返回布爾值

func deductCoins() -> Bool { 

     if price > coins { 
      label.text = String("You dont have enough coins") 
      self.view.addSubview(label) 
      return false 
     } 
     if coins >= price { 
      coins = coins - price 
      UserDefaults.standard.set(coins, forKey: "Coins") 

      //this stuff has to do with the purchasing 
      BackBtn.removeFromSuperview() 
      PurchaseBtn.removeFromSuperview() 
      label.removeFromSuperview() 
      image.removeFromSuperview() 
      return true 
     } 

     return true 
    } 

並且該功能用於扣除從UserDefaults值「硬幣」的硬幣(因此而得名),當用戶「購買內容「在我的遊戲中,我需要一種方法來測試函數deductCoins是否返回true或false,取決於用戶與項目的價格(如代碼中所述)相比較的硬幣數量,但是我需要測試if它在功能範圍外返回真或假

編輯

現在問題更加清楚了(P.S.當我寫下原件時,我感到很疲倦)

+4

什麼*「偉大工程」 *又是什麼*「我需要或確定多個值瞭解鎖不是」 *是什麼意思? –

+3

完全不清楚的問題.. – vaibhav

+0

一個人可以得到你在問什麼 –

回答

1

它與任何其他具有返回值的函數的工作方式相同。

let deducted = deductCoints() 
if deducted { 
    //function returned true 
} else { 
    //function returned false 
} 

如果你不想存儲返回值,只是檢查了一次,你甚至不需要將其存儲在一個變量。

if deductCoins() { 
    //function returned true 
} else { 
    //function returned false 
} 

有關功能的更多信息,請檢查Swift Programming Language Guide - Functions

+0

非常感謝多數人正是我所需要的,它的工作原理 –