UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: override can only be specified on class members?

Forums > Swift

I get this error on the following code line 30:

import UIKit

class Developer {
    var name: String? // ? is making it optional
    var jobTitle: String? // ? is making it optional
    var yearsExp: Int? // ? is making it optional

    init(name: String, jobTitle: String, yearsExp: Int) {
        self.name = name
        self.jobTitle = jobTitle
        self.yearsExp = yearsExp
    }
func speakName() {
        print("Developers name is: \(name!)") // ! is force unwrap
        print("Developers Job Title is: \(jobTitle!)") // ! is force unwrap
        print("Developers years Experience is: \(yearsExp!)") // ! is force unwrap
    }
}

class iOSDeveloper: Developer {
    var favoriteFramework: String? // ? is making it optional

    func speakFavoriteFramework() {
        if let favoriteFramework = favoriteFramework {
            print("Favorite framework is: \(favoriteFramework)")
        } else {
            print("No favorite framework found!")
        }

override func speakName() {
            print("\(name!) - \(jobTitle!)")

        }

    }

}

2      

hi Rodney,

your definition of speakName in the class iOSDeveloper appears within the body of the function speakFavoriteFramework, and so is local to that function.

define speakName at the same level as the definition of speakFavoriteFramework.

hope that helps,

DMG

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.