简体   繁体   中英

Access constant or variables from inside a function to in `viewdidload` to save in firebase swift 3

I am unable to access constant or variables which are defined inside a function for example i have defined a constant inside a didselectrow function now when i select a row the value of constant get printed in console but now i want to save that value in firebase so when i call that value in button it is undefined why? what should i do that??

here is the code i have written in button the constant countryGroup is undefined. because i have defined it in didselectrow

button

 @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)")
    }

Instead of defining let country = currentCell.textLabel!.text! in didselectrow i have defined a global variable var country = String? and set country = currentCell.textLabel!.text!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM