-5

I am getting my value as Value comes as optional("myvalue"),But how can i get only value without the optional. I have tried a lot but not getting point, any help will be appreciated highly. The thing is that i am using Firebase as database and getting values in database.

I want to get value of Gender, Blood and country code.


Firebase connectivity

 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])


Other code

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>

Console

   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
Xcodian Solangi
  • 2,342
  • 5
  • 24
  • 52
  • which value can you specify ? – KKRocks May 22 '17 at 10:47
  • Optionals are a key feature of Swift. They're explained in the Swift ebook. You cannot not learn this if you want to do Swift. https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html – Eric Aya May 22 '17 at 11:08
  • thanks @EricAya i am trying to focus on these concepts but still trying............. – Xcodian Solangi May 22 '17 at 11:09

2 Answers2

2

Try this :

Print object

print(bloodGroup ?? "")

And

cell.textLabel!.text = (dropDownList[indexPath.row] ?? "")
KKRocks
  • 8,222
  • 1
  • 18
  • 84
0

Please write

if tableView == tableViewB {

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

}

Instead of

if tableView == tableViewB {

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

}

In table view cellForRowAt method.