值来自可选(“myvalue”),如何在没有可选值的情况下获得唯一值

时间:2017-05-22 10:45:35

标签: ios swift console

我得到了我的价值,因为价值是可选的(“我的价值”),但是如何在没有可选的情况下获得价值。我已经尝试了很多,但没有得到重点,任何帮助将得到高度赞赏。问题是我使用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

2 个答案:

答案 0 :(得分:2)

试试这个:

打印对象

print(bloodGroup ?? "")

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

答案 1 :(得分:0)

请写下

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方法。

相关问题