Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

"Missing return in instance method expected to return 'Float'" Swift

I have this problem when I try to add print() in my override func. What should I do to avoid this?

screenshot is here

I tried to add some return but nothing happens.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need a return statement in the overridden function.

You can store the result of the call to super in a variable, do the print, then return the variable.

override func sum(_ first: Float, _ second: Float) -> Float {
    let result = super.sum(first, second)

    print("Message here")

    return result
}

Or you can print first and directly return the call to super:

override func sum(_ first: Float, _ second: Float) -> Float {
    print("Message here")

    return super.sum(first, second)
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading