gpt4 book ai didi

swift - 将 firebase 数据库值转换为 Int

转载 作者:搜寻专家 更新时间:2023-11-01 07:14:23 24 4
gpt4 key购买 nike

在我的应用程序中,人们可以对他们认为会获胜的球队下注。当他们去打赌时,应该将一些东西上传到数据库中。首先是下注者的用户名。这是哪个投注(是一注二注三),它应该显示投注的球队。

问题 我将赌注转换为 Int,这样每次有人下注时我都可以递增它,这样他们就有了唯一的赌注。

下面我要解释几件事,因为出于测试目的,我的代码现在很困惑。

在 viewDidLoad 方法中,我可以收集用户名和某人下注的次数。在数据库中,这默认为字符串“0”。

现在下注 self.betNumber 的值应该是“0”

然后在 submitData 函数中,我安全地获取 betNumber 并将其转换为 Int,然后将该值分配给全局声明的变量 bets

然后我将赌注增加 1之后我尝试将 self.bets 转换成一个字符串,但它作为一个 child 进入数据库

这是我遇到错误的地方

import UIKit
import Foundation
import Firebase


class addBets: UIViewController {
let betRef = FIRDatabase.database().reference(fromURL: "not showing these sorry")
let userRef = FIRDatabase.database().reference(fromURL: "")
let ref = FIRDatabase.database().reference(fromURL: "")
let currentUser = FIRAuth.auth()?.currentUser


@IBOutlet weak var teamOneLabelAdd: UILabel!
@IBOutlet weak var teamTwoLabelAdd: UILabel!

@IBOutlet weak var teamOneBet: UITextField!

@IBOutlet weak var teamTwoBet: UITextField!



var userName: String!
var teamOne: String!
var teamTwo = String()

var betNumber: String!
var bets: Int = 0




var teamOneBetAnotherOne: String!
var teamOneBetAnotherTwo: String!
override func viewDidLoad() {
super.viewDidLoad()
teamOneLabelAdd.text = teamOne
teamTwoLabelAdd.text = teamTwo


//Looks for single or multiple taps.
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")

//Uncomment the line below if you want the tap not not interfere and cancel other interactions.
tap.cancelsTouchesInView = false

view.addGestureRecognizer(tap)

//self.teamOneBet.delegate = self
//self.teamTwoBet.delegate = self

let userID = FIRAuth.auth()?.currentUser?.uid
ref.child("User").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
let value = snapshot.value as? NSDictionary
let username = value?["username"] as? String ?? ""
self.userName = username
let betnumber = value?["bets"] as? String ?? ""
self.betNumber = betnumber




// ...
}) { (error) in
print(error.localizedDescription)
}


}

func textFieldShouldReturn(userText: UITextField!) -> Bool {
teamOneBet.resignFirstResponder()
teamTwoBet.resignFirstResponder()
return true
}


func dismissKeyboard() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
view.endEditing(true)
}

@IBAction func submitData(_ sender: Any) {
// get the userName to tell who is doing the bet and set it as a key

sleep(4)

if let usernamee = self.userName {
if let anotherBet = self.betNumber as? Int {
self.bets = anotherBet
}
self.bets += 1

let usableBets = self.bets as? String ?? ""

let useRef = self.ref.child("Bets").child(usernamee).child(self.bets as? String ?? "")

var values = ["Username": usernamee,"Bet": "0", "ForTeam": ""]
if let betOne = self.teamOneBet.text{

if betOne == "" {

} else {
values["Bet"] = betOne
values["ForTeam"] = teamOneLabelAdd.text
}
}
if let betTwo = self.teamTwoBet.text{

if betTwo == "" {

} else {
values["Bet"] = betTwo
values["ForTeam"] = teamTwoLabelAdd.text
}
}


//self.betRef.child("Bets").setValue(self.userName)
if let worked = currentUser?.getTokenWithCompletion() {

useRef.setValue(values)
/*
useRef.updateChildValues(values, withCompletionBlock: { (error, ref) in
if (error != nil) {
print(error!)
return

}

})
*/



}


self.navigationController?.popViewController(animated: true)

}







}

func getUserName() {
let userID = FIRAuth.auth()?.currentUser?.uid
ref.child("User").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
let value = snapshot.value as? NSDictionary
let username = value?["username"] as? String ?? ""
self.userName = username

// ...
}) { (error) in
print(error.localizedDescription)
}
}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//let indexPath = self.tableView.indexPathForSelectedRow!
//var DestViewController : addBets = segue.destination as! addBets
//DestViewController.teamOne = self.teamOne.text!
//DestViewController.teamOne = self.teamTTwo

if segue.identifier == "gotobetview" {
if let destination = segue.destination as? BetView {

//destination.userNames = self.teamOne.text! as? String

//destination.teamTwo = (self.teamTwo.text! as? String)!
}
}

}

}

这里是 firebase 结构,我提取的数据来自“用户”,我写入的数据是“赌注”

enter image description here

最佳答案

粗略地看一下您的代码,错误似乎在这一行:

let useRef = self.ref.child("Bets").child(usernamee).child(self.bets as? String ?? "")

我不完全确定您想在哪里发布用户的投注,但这样做是将 useRef 设置为位于 Bets.\(usernamee) 的 child 。 1 下注为 1 时为 Bets.\(usernamee).2 下注为 2 等

如果您希望每次下注都是唯一的,只需将 useRef 更改为:

let useRef = self.ref.child("Bets").child(usernamee).childByAutoId()

这将在 firebase 中创建一个看起来像这样的新条目:

Bets:
usernameyoyo:
-Kfx81GvUxoHpmmMwJ9P:
Bet: "300",
ForTeam: "Natus Vincere",
Username: "usernameyoyo"

那么现在的问题是如何获得所下的赌注?嗯,这实际上很简单,因为每个人都是唯一标识的:

func getBetsFor(_ username: String) {
ref.child("Bets".child(username).observe(.childAdded, with: { snapshot in
//
// this is the unique identifier of the bet. eg, -Kfx81GvUxoHpmmMwJ9P
let betId = snapshot.key as String

guard let dict = snapshot.value as? [String: AnyHashable] else {
print("failed to get dictionary from Bets.\(username)")
return
}

let bet = dict["Bet"] as? String
let forTeam = dict["ForTeam"] as? String


// do something with the above information!

})
}

关于swift - 将 firebase 数据库值转换为 Int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43035987/

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