Reputation: 1479
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
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