Search code examples
swiftxcodestructure

Value of type "User" has no member 'logStatus" Swift Error


I am new to code and am trying to learn about Structures. I've created a structure and would like to know where I went wrong as it does not allow for me to call to the method logStatus that I created.

    let name: String
    var email: String
    var followers: Int
    var isActive: Bool
    
    
    init(name: String, email: String, followers: Int, isActive:Bool) {
        self.name = name
        self.email = email
        self.followers = followers
        self.isActive = isActive
        
       
        
        func logStatus(){
            if (isActive)  {
                print("\(name) is working hard" ) }

             else {
                print("\(name) has left Earth")
            }
            }
            }
}

var test = User(name: "Richard", email: "richard@gmail.com", followers: 0, isActive: false)

test.logStatus()


Solution

  • You should take logStatus() out of your initializer

    struct SomeStruct {
        let name: String
        var email: String
        var followers: Int
        var isActive: Bool
        
        init(name: String, email: String, followers: Int, isActive:Bool) {
            self.name = name
            self.email = email
            self.followers = followers
            self.isActive = isActive
        }
    
        func logStatus() {
            if (isActive) {
               print("\(name) is working hard" ) 
            } else {
               print("\(name) has left Earth")
            }
         }
    }