【问题标题】:Value comes as optional("myvalue"), how to get only value without the optional [duplicate]值作为可选(“myvalue”)出现,如何在没有可选的情况下仅获取值[重复]
【发布时间】: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,我正在努力专注于这些概念,但仍在尝试......

标签: ios swift console


【解决方案1】:

试试这个:

打印对象

print(bloodGroup ?? "")

cell.textLabel!.text = (dropDownList[indexPath.row] ?? "")

【讨论】:

  • 请查看 Firebase 连接的更新代码,它警告我删除???
  • 何时要求删除?
  • ??在您建议的一行中
  • 我建议打印并为标签赋值。
【解决方案2】:

请写

if tableView == tableViewB {

    let cell = tableViewB.dequeueReusableCell(withIdentifier: "blood", for: indexPath)
    cell.textLabel!.text = dropDownList[indexPath.row] as! String
    return cell

}

代替

if tableView == tableViewB {

    let cell = tableViewB.dequeueReusableCell(withIdentifier: "blood", for: indexPath)
    cell.textLabel!.text = dropDownList[indexPath.row] 
    return cell

}

在表格视图中 cellForRowAt 方法。

【讨论】:

  • 我更新了问题,请参阅 firebase 连接代码 plz
猜你喜欢
  • 1970-01-01
  • 2013-07-04
  • 2016-09-07
  • 2022-01-10
  • 2020-12-10
  • 1970-01-01
  • 2014-10-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多