4

Is this warning anything that I should be concerned about?

warning

If so what would be a solution? this is my function :

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destination = segue.destination as? ProfileViewController{
    let cell = sender as! UITableViewCell
        let selectedRow = myTableView.indexPath(for: cell)!.row


        switch (mySegmentedControl.selectedSegmentIndex){
        case 0:
            destination.nameVar = userSFList[selectedRow].name!
            destination.imageOneURL = userSFList[selectedRow].image!
            destination.bioVar = userSFList[selectedRow].bio!

            if let image2 = userSFList[selectedRow].imageTwo  {
               destination.imageTwoUrl = image2                }

            if let contactInt = userSFList[selectedRow].contact as? Int {
                destination.contact = contactInt
            }

            break

        case 1:

            destination.nameVar = userEBList[selectedRow].name!
            destination.imageOneURL = userEBList[selectedRow].image!
             destination.imageTwoUrl = userEBList[selectedRow].imageTwo!



            if let contactInt = userEBList[selectedRow].contact as? Int {
                destination.contact = contactInt
            }


            break
        case 2:
            destination.nameVar = userSFOList[selectedRow].name!
            destination.imageOneURL = userSFOList[selectedRow].image!

            if let contactInt = userSFOList[selectedRow].contact as? Int {
                destination.contact = contactInt
            }

            break
        case 3:
            destination.nameVar = userSJList[selectedRow].name!
            destination.imageOneURL = userSJList[selectedRow].image!
                     if let contactInt = userSJList[selectedRow].contact as? Int {
                destination.contact = contactInt
            }
            break
        default:
            break

    }
}

}

I am using a segmented control with four different segments and pulling the data using firebase.

Step Step
  • 97
  • 1
  • 12
  • 1
    try doing something like: `destination.contact = userEBList[selectedRow].contact.intValue` ; unless it's an optional and then you'd probably need to do something like `destination.contact = userEBList[selectedRow].contact.intValue ?? 0` (i.e. a default of zero). – Michael Dautermann Apr 08 '17 at 01:48
  • Why do you have the same code repeated 3 times? – Alexander Apr 08 '17 at 01:53
  • @Alexander It looks like three different cases: userEBList, userSJList and userSFOList. – backslash-f Apr 08 '17 at 01:59
  • @backslash-f So there should be a single variable that's conditionally set to one of those 3, and then the repeated code is done just once, to that variable – Alexander Apr 08 '17 at 02:23
  • @Alexander I agree. – backslash-f Apr 08 '17 at 02:27
  • @Alexander I am using a segmented control with a table view, the segmented control has 4 different userLists (SF, EB, SFO, SJ), I'm using switch statement to send my user information to a profileViewController, so you are suggesting I put the code from each case into 4 different variables? – Step Step Apr 08 '17 at 03:12
  • @hghg I would show you what I mean, but you haven't posted the code. I'm not gonna bother typing it out myself. This is why we have a policy against screenshots of code :) – Alexander Apr 08 '17 at 03:25
  • @Alexander just fyi the code is not done and I still have more to add. – Step Step Apr 08 '17 at 04:35

1 Answers1

6

My personal rule is always zero warnings.
Better safe than sorry.

Is contact an Optional? If so...

You could use Optional Binding:

if let contactInt = userSFOList[selectRow].contact as? Int {
  destination.contact = contactInt
}

Or the Nil-Coalescing Operator:

destination.contact = userSFOList[selectedRow].contact.intValue ?? <Your default Int here>

You could also use guard as pointed out by @Kamil.S, like:

guard let nameVar = userSFOList[selectedRow].name,
  let imageVar = userSFOList[selectedRow].image,
  let contactVar = contact as? Int else {
    // Conditions were failed. `return` or `throw`.
  }

destination.nameVar = nameVar
destination.imageOneURL = imageVar
destination.contact = contactVar
backslash-f
  • 7,923
  • 7
  • 52
  • 80