rattletrap99
rattletrap99

Reputation: 1479

Return missing from this function?

I'm getting this error "Missing return in a function expected to return 'NSAttributedString?'"

Can someone please help? I'm beating my small brain--prone to idiot mistakes--out over this thing. I put the "dummyString" line in to ensure there was a return, but no luck.

TIA

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {

    let dummyString = "Dummy String"


    if component == 1 {
        return NSAttributedString(string: denomArray[row], attributes: [NSAttributedString.Key.foregroundColor: UIColor.yellow])

    }else if component == 0 {
        if issuerArray [row] == "US" {
            return NSAttributedString(string: self.issuerArray[row], attributes: [NSAttributedString.Key.foregroundColor: BaseViewController.Flag.us.themeColor])
        }
        if issuerArray [row] == "Canada" {
            return NSAttributedString(string: self.issuerArray[row], attributes: [NSAttributedString.Key.foregroundColor: BaseViewController.Flag.canada.themeColor])
        }

    }else{
        return NSAttributedString(string: dummyString, attributes: [NSAttributedString.Key.foregroundColor: UIColor.red])

    }
}

Upvotes: 0

Views: 35

Answers (2)

Paulw11
Paulw11

Reputation: 115051

The problem is the construct of your if component == 0 block. If the country is neither US nor Canada then there is no return. You may know that the row can only contain one of those two values, but the compiler doesn't.

The simplest fix is to make the default return non-conditional. That way, if no previous return has been executed, you will return the default value.

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {

    let dummyString = "Dummy String"

    if component == 1 {
        return NSAttributedString(string: denomArray[row], attributes: [NSAttributedString.Key.foregroundColor: UIColor.yellow])

    } else if component == 0 {
        if issuerArray [row] == "US" {
            return NSAttributedString(string: self.issuerArray[row], attributes: [NSAttributedString.Key.foregroundColor: BaseViewController.Flag.us.themeColor])
        }
        if issuerArray [row] == "Canada" {
            return NSAttributedString(string: self.issuerArray[row], attributes: [NSAttributedString.Key.foregroundColor: BaseViewController.Flag.canada.themeColor])
        }
    }
    return NSAttributedString(string: dummyString, attributes: [NSAttributedString.Key.foregroundColor: UIColor.red])

}

Upvotes: 2

Frankenstein
Frankenstein

Reputation: 16371

You just need to add return nil above the last line.

Upvotes: 1

Related Questions