2017-09-19 63 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

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