Reputation: 1278
I have a custom UITextfield in swift called COSTextField. It is something like
class COSTextField: UITextField {
func customFont() -> UIFont? {
if let font = self.font {
return UIFont(name: kCOSFontRegular, size: font.pointSize)
}
}
}
I am trying to extend this COSTextField class and override the customFont method. My new class looks like
class COSTextFieldLight: COSTextField {
override func customFont() -> UIFont? {
if let font = self.font {
return UIFont(name: kCOSFontLight, size: font.pointSize)
}
}
}
When I try to compile this, I get an error Missing return in a function expected to return 'UIFont?'
. If I add a return nil statement at the end of my override function, I get an error Non-void function should return a value
What am I doing wrong?
Upvotes: 1
Views: 580
Reputation: 70112
Add return nil
just after the if let
branch:
class COSTextField: UITextField {
func customFont() -> UIFont? {
if let font = self.font {
return UIFont(name: kCOSFontRegular, size: font.pointSize)
}
return nil
}
}
class COSTextFieldLight: COSTextField {
override func customFont() -> UIFont? {
if let font = self.font {
return UIFont(name: kCOSFontLight, size: font.pointSize)
}
return nil
}
}
That way, if the if let
condition fails, your function will return nil.
Upvotes: 3