1

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?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
tbag
  • 1,268
  • 2
  • 16
  • 34
  • 1
    you will need an `else` part of the optional binding `if let font = self.font { ...`, because if that fails, you will not return anything. – Dániel Nagy Oct 01 '15 at 11:48
  • I tried adding a `return nil` statement right after the `if let font = self.font { ...`. But that too gives me an error `Non-void function should return a value` – tbag Oct 01 '15 at 11:50
  • I meant `if let font = self.font {...} else { return nil }` – Dániel Nagy Oct 01 '15 at 11:52

1 Answers1

3

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.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • Yeah I figured that is it. But I am still trying to understand why it only complains about missing return in the override function and not in the original `func customFont() -> UIFont?` function – tbag Oct 01 '15 at 12:01
  • Your answer is correct of course. Something odd though. If you type the above functions into a Playground without the `return nil` statements, and then add them, it will show an error `Non-void function should return a value`. Deleting them and adding the functions back as a whole makes it work. Some problem with the incremental compile. – vacawama Oct 01 '15 at 12:02
  • @EricD. for me it only complains on the override function when I remove the `return nil` line – tbag Oct 01 '15 at 12:11
  • I started seeing an error after I cleaned the project. Thanks for helping out! – tbag Oct 01 '15 at 16:00
  • @EricD. I'm having an issue which may be related to font, I'm really stumped, if you could take a look if you have any ree time: http://stackoverflow.com/questions/34285246/heightforrowatindexpath-crashing-intermittently – user2363025 Dec 15 '15 at 10:40