【发布时间】:2017-05-22 10:45:35
【问题描述】:
我得到了我的价值,因为价值是可选的(“myvalue”),但是我如何才能在没有可选的情况下只获得价值。我已经尝试了很多但没有得到任何帮助,我们将不胜感激。问题是我使用 Firebase 作为数据库并在数据库中获取值。
我想获取性别、血统和国家代码的值。
Firebase 连接
ref.child("user_registration").child(UserID!).setValue(["username": fullName.text!, "email": emailTextField.text!, "contact": numberText.text!, "city": myCity.text!, "state":countryText.text!, "gender": genderGroup, "blood": bloodGroup,"code": countryGroup])
其他代码
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == tableViewB {
let cell = tableViewB.dequeueReusableCell(withIdentifier: "blood", for: indexPath)
cell.textLabel!.text = dropDownList[indexPath.row]
return cell
}
else if tableView == genderT {
let cell = genderT.dequeueReusableCell(withIdentifier: "gender", for: indexPath)
cell.textLabel!.text = genderL[indexPath.row]
return cell
}
else {
let cell = countryT.dequeueReusableCell(withIdentifier: "country", for: indexPath) as! CountryTableViewCell
cell.countryLabel.text = countryL[indexPath.row]
cell.flagImage.image = countryFlags[indexPath.row]
return cell
}
}
var bloodGroup : String?
var genderGroup : String?
var countryGroup : String?
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
bloodGroup = currentCell.textLabel!.text!
print("\(bloodGroup)")
}
else if tableView == genderT {
let selectedDataG = genderL[indexPath.row]
genderB.setTitle(selectedDataG, for: .normal)
self.genderT.isHidden = true
self.flag = 1
let indexPath = genderT.indexPathForSelectedRow
let currentCell = genderT.cellForRow(at: indexPath!)! as UITableViewCell
genderGroup = currentCell.textLabel!.text!
print("\(genderGroup)")
}
else {
let selectedDataG = countryL[indexPath.row]
countryB.setTitle(selectedDataG, for: .normal)
self.countryT.isHidden = true
self.flag = 1
let indexPath = countryT.indexPathForSelectedRow
let currentCell = countryT.cellForRow(at: indexPath!)! as! CountryTableViewCell
countryGroup = currentCell.countryLabel.text!
let finalFlag = currentCell.flagImage.image!
print("\(finalFlag)" + "\(countryGroup)")
}
}
<br>
控制台
com.apple.MobileAsset.TextInput.SpellChecker
Optional("male")
Optional("A+")
<UIImage: 0x600000295950>, {96, 64}Optional("+92")
fatal error: unexpectedly found nil while unwrapping an Optional value
【问题讨论】:
-
你可以指定哪个值?
-
选项是 Swift 的一个关键特性。它们在 Swift 电子书中进行了解释。如果你想做 Swift,你不能不学习这一点。 developer.apple.com/library/content/documentation/Swift/…
-
感谢@EricAya,我正在努力专注于这些概念,但仍在尝试......