2017-04-17 69 views
0

我無法訪問常量或它們例如函數中定義的變量我已經定義了一個常數didselectrow函數內部現在,當我選擇一行常量的值獲得印在控制檯,但現在我想保存firebase該值,所以當我在button稱之爲價值是不確定的,爲什麼?我該怎麼做?

訪問常量或函數裏面的變量保存在火力迅速3

這裏是我寫在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)") 
    } 
+0

,因爲它變成了局部變量而不是實例變量。定義變量外的函數。 –

+0

我應該怎麼做才能在Firebase中檢索? –

+0

聲明讓selectedData:Int?外表函數和內功能只是用 - :selectedData = DROPDOWNLIST [indexPath.row] –

回答

0

代替在didselectrow限定let country = currentCell.textLabel!.text!的我已經定義一個全局變量var country = String?並設置country = currentCell.textLabel!.text!