我无法访问函数内定义的常量或变量,例如,我现在在 didselectrow
函数内定义了一个常量,当我选择一行时,常量的值会打印在控制台中,但是现在我想将该值保存在 firebase
中,所以当我在 button
中调用该值时,它是未定义的,为什么?我该怎么办?
这是我在button
中编写的代码,常量countryGroup
未定义。因为我已经在 didselectrow
按钮
@IBAction func createAccountAction(_ sender: Any) {
ref.child("user_registration").child(UserID!).setValue(["username": fullName.text, "email": emailTextField.text, "contact": numberText.text, "city": myCity.text, "state": countryText.text, "country": country])
}
didselectrow
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView == tableViewB {
let selectedData = dropDownList[indexPath.row]
buttonB.setTitle(selectedData, for: .normal)
self.tableViewB.isHidden = true
self.flag = 1
let indexPath = tableViewB.indexPathForSelectedRow
let currentCell = tableViewB.cellForRow(at: indexPath!)! as UITableViewCell
let country = currentCell.textLabel!.text!
print("\(country)")
}
最佳答案
我没有在 didselectrow
中定义 letcountry = currentCell.textLabel!.text!
,而是定义了一个全局变量 varcountry = String?
并设置country = currentCell.textLabel!.text!
关于ios - 从函数内部访问常量或变量到 `viewdidload`以保存在firebase swift 3中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43451107/