Skip to content

Instantly share code, notes, and snippets.

@TheMuellenator
Last active June 10, 2025 05:57
Show Gist options
  • Save TheMuellenator/569463645ec81ea49f85ebf6d6dd99fe to your computer and use it in GitHub Desktop.
Save TheMuellenator/569463645ec81ea49f85ebf6d6dd99fe to your computer and use it in GitHub Desktop.
iOS repl.it - Structures Challenge Solution
// Define a struct
struct User {
var name: String
var email: String?
var followers: Int
var isActive: Bool
func logStatus() {
if (isActive) {
print("\(name) is working hard")
} else {
print("\(name) has left earth")
}
}
}
// Initialise the struct
var branson = User(name: "Richard", email: nil, followers: 0, isActive: false)
branson.logStatus()
// Diagnostic code - do not change this code
print("\nDiagnostic code (i.e., Challenge Hint):")
var musk = User(name: "Elon", email: "[email protected]", followers: 2001, isActive: true)
musk.logStatus()
print("Contacting \(musk.name) on \(musk.email!) ...")
print("\(musk.name) has \(musk.followers) followers")
// sometime later
musk.isActive = false
musk.logStatus()
@Shah0651
Copy link

Shah0651 commented Oct 26, 2024

func exerciseStruct() {
    struct User{
    // Define the User struct here
        var name: String
        var email: String?
        var followers: Int
        var isActive: Bool
        
        

    // Initialise a User struct here
        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 == true{
                print("\(name) is working hard")
            }else {
                print("\(name) has left earth")

            }
        }


    }
    // Diagnostic code - do not change this code
    
    var newUser = User(name: "Richard", email: nil, followers: 0, isActive: false)
    newUser.logStatus()
    
    print("\nDiagnostic code (i.e., Challenge Hint):")
    var musk = User(name: "Elon", email: "[email protected]", followers: 2001, isActive: true)
    musk.logStatus()
    print("Contacting \(musk.name) on \(musk.email!) ...")
    print("\(musk.name) has \(musk.followers) followers")
    // sometime later
    musk.isActive = false
    musk.logStatus()
    
}
// Don't modify this code
exerciseStruct()

@PatrycjaOosthuizen
Copy link

My solution:
// ✅ Change name exerciseStruct() to exercise()

func exercise() {
struct User {
var 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")
        }
    }
}

// Diagnostic code - do not change this code
let newUser = User(name: "Richard", email: nil, followers: 0, isActive: false)
newUser.logStatus()

print("\nDiagnostic code (i.e., Challenge Hint):")
var musk = User(name: "Elon", email: "[email protected]", followers: 2001, isActive: true)
musk.logStatus()
print("Contacting \(musk.name) on \(musk.email!) ...")
print("\(musk.name) has \(musk.followers) followers")

// Some time later
musk.isActive = false
musk.logStatus()

}

// ✅ Don't call exercise(), let the test suite call it. This prevents the error.

@Saquib504
Copy link

Here is the correct solution,
I guess maybe some people are not capitalising "B" in "Bool", due to which they are facing problems

func exercise() {

// Define the User struct here
struct User{
    let name: String
    var email: String?
    var followers: Int
    var isActive: Bool
    
    func logStatus(){
        if isActive{
            print("\(name) is working hard")
        }else{
            print("\(name) has left earth")
        }
        
    }
}



// Initialise a User struct here
var user = User(name: "Richard", followers: 0, isActive: false)
user.logStatus()




// Diagnostic code - do not change this code
print("\nDiagnostic code (i.e., Challenge Hint):")
var musk = User(name: "Elon", email: "[email protected]", followers: 2001, isActive: true)
musk.logStatus()
print("Contacting \(musk.name) on \(musk.email!) ...")
print("\(musk.name) has \(musk.followers) followers")
// sometime later
musk.isActive = false
musk.logStatus()

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment