NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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!)")

        }

    }

}

   

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

   

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

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

Reply to this topic…

You need to create an account or log in to reply.

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.