gpt4 book ai didi

swift - 使用单个函数 Swift 3 设置多个 UserDefaults 值

转载 作者:行者123 更新时间:2023-11-30 12:08:56 25 4
gpt4 key购买 nike

我有一个游戏,我试图保持它的小尺寸,在我的游戏中我有这个功能

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")
BackBtn.removeFromSuperview()
PurchaseBtn.removeFromSuperview()
label.removeFromSuperview()
image.removeFromSuperview()
return true
}

return true
}

此函数用于购买游戏内内容是否有办法传入参数或方法来根据变量将不同的 UserDefault Boolean 值更改为 true例如在伪代码

func deductCoins() -> Bool {
if price is greater than coins {

tell the user that he/she doesnt have enough coins
}
if coins is greater than coins {
tell the user he/she has bought the item
change a UserDefault Boolean value
if a value is different based on a parameter
change a different value because of it
}
}

我不太擅长编写长而复杂的函数,我用这个函数只解锁了 ViewController 中的一项,但现在我正在使用 CollectionView 单元格,其中包含不同的项,我想要仅使用一种功能根据用户选择的内容解锁不同的内容

-更新1-

这是我想出的方法,它绝对不是最好的方法,但它有效

//this Int value is set in the Index method in my collectionView 
var Int = 0

func deductCoins() -> Bool {

if price > coins {
label.text = String("You dont have enough coins")
self.view.addSubview(label)
return false
}
if coins >= price {
if Int == 1 {
coins = coins - price
UserDefaults.standard.set(coins, forKey: "Coins")
BackBtn.removeFromSuperview()
PurchaseBtn.removeFromSuperview()
label.removeFromSuperview()
image.removeFromSuperview()
UserDefaults().set(true, forKey: "ItemUnlocked")
return true
}
if Int == 2 {
coins = coins - price
UserDefaults.standard.set(coins, forKey: "Coins")
BackBtn.removeFromSuperview()
PurchaseBtn.removeFromSuperview()
label.removeFromSuperview()
image.removeFromSuperview()
UserDefaults().set(true, forKey: "ItemUnlocked")
return true
}
if Int == 3 {
coins = coins - price
UserDefaults.standard.set(coins, forKey: "Coins")
BackBtn.removeFromSuperview()
PurchaseBtn.removeFromSuperview()
label.removeFromSuperview()
image.removeFromSuperview()
UserDefaults().set(true, forKey: "ItemUnlocked")
return true
}
if Int == 4 {
coins = coins - price
UserDefaults.standard.set(coins, forKey: "Coins")
BackBtn.removeFromSuperview()
PurchaseBtn.removeFromSuperview()
label.removeFromSuperview()
image.removeFromSuperview()
UserDefaults().set(true, forKey: "ItemUnlocked")
return true
}
if Int == 5 {
coins = coins - price
UserDefaults.standard.set(coins, forKey: "Coins")
BackBtn.removeFromSuperview()
PurchaseBtn.removeFromSuperview()
label.removeFromSuperview()
image.removeFromSuperview()
return true
}
}

return true
}

这是索引路径方法

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// handle tap events
if indexPath.item == 0 {
//the first item in my Index path is free it cost nothing
//other things happen here i didnt post on purpose to keep some privacy
}
if indexPath.item == 1 {
Int = 1
//other things happen here i didnt post on purpose to keep some privacy
}
if indexPath.item == 2 {
Int = 2
//other things happen here i didnt post on purpose to keep some privacy

}
if indexPath.item == 3 {
Int = 3
//other things happen here i didnt post on purpose to keep some privacy

}
if indexPath.item == 4 {
Int = 4
//other things happen here i didnt post on purpose to keep some privacy

}
if indexPath.item == 5 {
Int = 5
//other things happen here i didnt post on purpose to keep some privacy
}
}

这就是我想出的,它的工作原理就像我也需要它,如果你们有任何其他更好的方法,请给我一个答案,我会检查一下

最佳答案

如果我理解正确,我假设您将在游戏中拥有元素列表,并且当用户购买每个元素时,您必须将购买元素标记设置为true并减少数量总硬币中的硬币。

我还假设您拥有类似的游戏数据

let gameList = [["level":"1", "name":"Saga","itemId":"10001","coinsRequired":10],
["level":"2", "name":"Rita","itemId":"10002","coinsRequired":20],
["level":"3", "name":"Villa","itemId":"10003","coinsRequired":10],
["level":"4", "name":"Corn","itemId":"10004","coinsRequired":5],
["level":"5", "name":"Leavy","itemId":"10005","coinsRequired":34],
["level":"6", "name":"Uhoo","itemId":"10006","coinsRequired":12]]

现在您可以实现您的游戏购买

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")

let gameItem = gameList[indexPath.row]
cell?.textLabel?.text = gameItem["name"] as? String

let button = UIButton(type: .custom)
button.frame = CGRect(x: 0, y: 0, width: 60, height: 40)
let gameStatus = UserDefaults.value(forKey: "ItemUnlocked\(gameItem["itemId"] as! Int)") as! Bool
if gameStatus == true{
// play the game
button.titleLabel?.text = "Play"
}else{
// buy the item
button.titleLabel?.text = "Buy\n $\(String(describing: gameItem["coinsRequired"]!))"
}
button.titleLabel?.numberOfLines = 0
button.tag = indexPath.row
button.addTarget(self, action: #selector(buyItemButtonTapped(_:)), for: .touchUpInside)
cell?.accessoryView = button
return cell!
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return gameList.count
}


func buyItemButtonTapped(_ aButton: UIButton){
let gameItem = gameList[aButton.tag]
buyIyem(itemId: gameItem["itemId"] as! Int)
}


@discardableResult
func buyIyem(itemId aItemId: Int) -> Bool {
let price = gameList[aItemId]["price"] as! Int
var totalCoins = UserDefaults.standard.integer(forKey: "TOTAL_COINS")
if price > totalCoins {
// SHow "You dont have enough coins".
return false
}
if totalCoins >= price {
totalCoins = totalCoins - price
UserDefaults.standard.set(totalCoins, forKey: "TOTAL_COINS")
// handle the UI here
UserDefaults().set(true, forKey: "ItemUnlocked\(aItemId)")
return true
}
return true
}

希望这对您的实现有所帮助

关于swift - 使用单个函数 Swift 3 设置多个 UserDefaults 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46317278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com