Skip to content

Instantly share code, notes, and snippets.

@TheMuellenator
Last active February 5, 2024 20:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • 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: "elon@tesla.com", 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()
@Hamm3rH3ad3r
Copy link

Hi! I thought she said that we "must" first initialize the struct, but I see that the answer provided worked without it. Can someone explain in what cases the initializers must really be used? I feel like by creating the var Richard with all its parameters is initializing the struct at the time of creating it. Thanks!

^^ I have the same question.

So I was really confused on this too, but I think I figured it out. If you only need one instance of the variable, so for this project you only need Richard, then you don't have to initialize it. Lets say that it was actually a social media site that had more than 1 person, then it would be much more time efficient to initialize the struct so that you could use it as a template.
TLDR; Initializing is used so that you can make a template for a struct

@StoraDamen
Copy link

to jacksonE1234 and others
https://www.youtube.com/watch?v=ObIxxHy8yY8
the answer to all questions on classes and structs ( no init needed in structs)

@giorgiokk
Copy link

Tip - make sure all the data types are capitalized and if the repl.it program doesnt make some things blue... proceed regards . for example I type Bool and instead of the the data type being highlight in blue text it stayed black .... and that what messed me up .

heres my answer

// TODO: Define the struct

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 this earth")
  }
}

}

// TODO: Initialise the struct

var richard = User(name: "richard", email: "yorich@gmail.com", followers: 102, isActive: true)

richard.logStatus()

@jihredoy
Copy link

TODO: Define the struct

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

TODO: Initialise the struct

var test = User(name: "Richard", email: nil, followers: 0, isActive: false) test.logStatus()

@verebro
Copy link

verebro commented May 12, 2020

Can anyone explain to me (cuz as far as I recall, Angela didn't), why the F " if (isActive) " is in parentheses???

@Burcugul123
Copy link

The answer here doesn't correspond with instructions from Angela. It's missing initializers.

My solution failed. I think the problematic part is the function part but I can't figure out why it isn't working. Can anyone understand what is wrong with my solution code?

// TODO: Define the struct

struct User {
let name: String
var email: String?
var followers: Int
var isActive : bool

// TODO: Initialise the struct

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.bool.true
{print("(name) is working hard")}
else { print("(name) has left earth")
}
}
}

var musk = User(name:"Richard", followers: 0, isActive: false)

print(musk.logStatus)

@cejche
Copy link

cejche commented May 25, 2020

why is this giving me an error? :(

// TODO: Define the 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 eart")}
}

}

// TODO: Initialise the struct

var newUser = User (name: "Richard", email: nil, followers: 0, isActive: false)
newUser.logStatus ()

The error I get says:

exit status 1
main.swift:3:13: error: use of undeclared type 'string'
var name: string
^~~~~~

and so with all the var I have

@cjmccaskill
Copy link

cejche, you are getting that error due to the "string" not being Capitalized.

@cejche
Copy link

cejche commented May 25, 2020

cejche, you are getting that error due to the "string" not being Capitalized.

Thank you! I noticed right after I posted lol

@cjmccaskill
Copy link

cejche, I am glad to be able to help someone out. I am just now learning the program along with you and have had my far sure of duh moments. It is always the simple ones that get you.

@vivekkumar198
Copy link

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 == true{
print("(name) is working hard")
}
else {
print("(name) has left earth")
}
}

}

// TODO: Initialise the struct

var Hendricks = User(name: "Richard", email:nil, followers:0, isActive:false)
Hendricks.logStatus()

// Challenge Hint below
// Diagnostic code - do not change this code

print("\nDiagnostic code (i.e., Challenge Hint):")
var musk = User(name: "Elon", email: "elon@tesla.com", 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()

@gyos23
Copy link

gyos23 commented Jun 4, 2020

Here's my solution of which I'm sure its like everyone else's. The one thing to note was the slight difficulty in 'bool' vs 'Bool' on repl.it. Only after exporting code to playground was I able to realize that repl.it was suggesting the wrong keyword and so I was coming up with errors.

Screen Shot 2020-06-04 at 9 16 40 AM

Screen Shot 2020-06-04 at 9 16 21 AM

// TODO: Define the struct
struct User {
let name: String
var email: String?
var followers: Int
var isActive: Bool

// TODO: Initialise the struct
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")
}
}
}

var user1 = User(name: "Richard", email: "", followers: 0, isActive: false)

user1.logStatus()

@hoover13
Copy link

hoover13 commented Jun 9, 2020

Hi! I thought she said that we "must" first initialize the struct, but I see that the answer provided worked without it. Can someone explain in what cases the initializers must really be used? I feel like by creating the var Richard with all its parameters is initializing the struct at the time of creating it. Thanks!

I am having the same thought too.... even if I write the code without initialiser , it still works. so wonder what is the point of writing it

@hiwihaile
Copy link

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

   }
}

}

var anotherUser = User(name:"Richard", email: nil , followers: 0 , isActive :false)

print(anotherUser.logStatus())

// Diagnostic code - do not change this code
print("\nDiagnostic code (i.e., Challenge Hint):")
var musk = User(name: "Elon", email: "elon@tesla.com", 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()

@GridPen
Copy link

GridPen commented Jul 16, 2020

Here is my one!

// TODO: Define the struct

struct User {
    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")
        }
    }
}

// TODO: Initialise the struct

var newUser = User(name: "Richard", email: nil, followers: 0, isActive: false)
newUser.logStatus()

// Challenge Hint below

// Diagnostic code - do not change this code
print("\nDiagnostic code (i.e., Challenge Hint):")
var musk = User(name: "Elon", email: "elon@tesla.com", 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()

@PassionForCoding
Copy link

//CAN ANYONE CHECK MY CODE PLEASE, I WOULD REALLY APPRECIATE..

import UIKit

struct userInfo {

let name: String
var email: String
var followers: Int
var IsActive: String

init(name: String, email: String, followers: Int, IsActive: String) {
    self.name = name
    self.email = email
    self.followers = followers
    self.IsActive = IsActive
}

func logStatus () {
    if IsActive == "Active" {
    print("\(name) is working Hard.")
        
    }
        else {
            
            print("\(name) has left earth")
        }
}

}

var user = userInfo(name: "Richard", email: "elon@tesla.com", followers: 0, IsActive: "InActive")
user.logStatus()

var user2 = userInfo(name: "Elon", email: "elon@tesla.com", followers: 2001, IsActive: "Active")
user2.logStatus()
print("Contacting (user2.name) on (user2.email)")
print("(user2.name) has (user2.followers) followers")

user2.IsActive.append("InActive")
user2.logStatus()

@bhaveshtandel17
Copy link

bhaveshtandel17 commented Sep 28, 2020

Hi! I thought she said that we "must" first initialize the struct, but I see that the answer provided worked without it. Can someone explain in what cases the initializers must really be used? I feel like by creating the var Richard with all its parameters is initializing the struct at the time of creating it. Thanks!

It is because Memberwise Initializers for Structure Types, See swift docs for more. https://docs.swift.org/swift-book/LanguageGuide/Initialization.html

@IAMCrazyCat
Copy link

Can anyone explain to me (cuz as far as I recall, Angela didn't), why the F " if (isActive) " is in parentheses???

I think the author used to work on JAVA a lot lol

@stojkovicstefan
Copy link

func exercise() {

// Define the User struct here
struct User{
    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")
    }
    }
    
    
}




let userName = User(name: "Richard", email: "richard@gmail.com", followers: 0, isActive: false)
userName.logStatus()

// Diagnostic code - do not change this code
print("\nDiagnostic code (i.e., Challenge Hint):")
var musk = User(name: "Elon", email: "elon@tesla.com", 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()

}

WHat did i do wrong?

@GridPen
Copy link

GridPen commented Nov 29, 2020

func exercise() {

// Define the User struct here
struct User{
    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")
    }
    }
    
    
}




let userName = User(name: "Richard", email: "richard@gmail.com", followers: 0, isActive: false)
userName.logStatus()

// Diagnostic code - do not change this code
print("\nDiagnostic code (i.e., Challenge Hint):")
var musk = User(name: "Elon", email: "elon@tesla.com", 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()

}

WHat did I do wrong?

Hold tight, let me check first.

@SonnyMorales
Copy link

SonnyMorales commented Dec 10, 2020

//CAN ANYONE CHECK MY CODE PLEASE, I WOULD REALLY APPRECIATE..

import UIKit

struct userInfo {

let name: String
var email: String
var followers: Int
var IsActive: String

init(name: String, email: String, followers: Int, IsActive: String) {
    self.name = name
    self.email = email
    self.followers = followers
    self.IsActive = IsActive
}

func logStatus () {
    if IsActive == "Active" {
    print("\(name) is working Hard.")
        
    }
        else {
            
            print("\(name) has left earth")
        }
}

}

var user = userInfo(name: "Richard", email: "elon@tesla.com", followers: 0, IsActive: "InActive")
user.logStatus()

var user2 = userInfo(name: "Elon", email: "elon@tesla.com", followers: 2001, IsActive: "Active")
user2.logStatus()
print("Contacting (user2.name) on (user2.email)")
print("(user2.name) has (user2.followers) followers")

user2.IsActive.append("InActive")
user2.logStatus()

You are setting the data type from: isActive to String, when in fact you have to set it to Bool (True/False).
Also, in the logStatus function, you are literally comparing the isActive to "Active".

Here you should not put literally the String "Active"
Try correcting it as follows

if isActive {
print("(name) is working Hard.")
}else{
print("(name) has left earth")
}

And sets the Email data type to an optional string:

ie.
var email : String?

@Dimanikin
Copy link

func exercise() {

// Define the User struct here
struct User {
    let name: String
    let email: String?
    let 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
let richard = User(name: "Richard", email: nil, followers: 0, isActive: false)

richard.logStatus()

// Diagnostic code - do not change this code
print("\nDiagnostic code (i.e., Challenge Hint):")
var musk = User(name: "Elon", email: "elon@tesla.com", 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()

}

@Dimanikin
Copy link

Hi! I thought she said that we "must" first initialize the struct, but I see that the answer provided worked without it. Can someone explain in what cases the initializers must really be used? I feel like by creating the var Richard with all its parameters is initializing the struct at the time of creating it. Thanks!

For structs, an initializer is created automatically (by default). And for classes, you always need to create an initializer manually.

@Dimanikin
Copy link

The answer here doesn't correspond with instructions from Angela. It's missing initializers.

My solution failed. I think the problematic part is the function part but I can't figure out why it isn't working. Can anyone understand what is wrong with my solution code?

// TODO: Define the struct

struct User {
let name: String
var email: String?
var followers: Int
var isActive : bool

// TODO: Initialise the struct

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.bool.true
{print("(name) is working hard")}
else { print("(name) has left earth")
}
}
}

var musk = User(name:"Richard", followers: 0, isActive: false)

print(musk.logStatus)

First: For structs, an initializer is created automatically (by default). And for classes, you always need to create an initializer manually.

second, that you have a boolean with a lowercase letter.

Third, the If Condition checks the value for truth. You can just write

If isActive {
print("lalala")
}

@HasanYazbeck
Copy link

struct User {
var name: String
var email : String?
var followers : Int
var isActive: Bool

 // Should mutate the method logStatus to use the same initiated struct
  mutating func logStatus()
  {
    if(isActive)
    {
      print("\(name) is working hard")
    }else
    {
     print("\(name) has left earth")
    }
  }
}

// Initialise a User struct here
var hasan = User(name: "Hasan" , email:"hasan@gmail.com" , followers: 40 , isActive: true)
hasan.logStatus()

@bnbrwd
Copy link

bnbrwd commented Nov 13, 2021

this is my solution

func exercise() {

// Define the User struct here

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

// Initialise a User struct here

    let userObj = User(name: "Richard", email: nil, followers: 0, isActive: false)
    userObj.logStatus()



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

}

@DozeStrawberry
Copy link

func exercise() {

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

// Initialise a User struct here
let message = User(name: "Sun", email: "Hiyou@gmail.con", followers: 150, isActive: false)
message.logStatus()


// Diagnostic code - do not change this code
print("\nDiagnostic code (i.e., Challenge Hint):")
var musk = User(name: "Elon", email: "elon@tesla.com", 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()

}

exercise()

@Stapa1
Copy link

Stapa1 commented Jun 14, 2022

func exercise() {

// Define the User struct here
struct User{
    let name: String
    let email: String?
    var followers: Int
    var isActive: Bool

func logStatus(){
if (isActive == true) {
print("(name) is working hard")
}else{
print("(name) has left earth")
}
}

}

// Initialise a User struct here
var Richard = User(name: "Richard", email: "Richard@yahoo,com", followers: 0, isActive: false)

Richard.logStatus()

// Diagnostic code - do not change this code
print("\nDiagnostic code (i.e., Challenge Hint):")
var musk = User(name: "Elon", email: "elon@tesla.com", 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()

}

@NavakanthMesala
Copy link

func exercise() {

// Define the User struct here
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" )
       }
   }
   
   init(name:String, email: String?, followers: Int, isActive: Bool){
       self.name = name
       self.email = email 
       self.followers = followers
       self.isActive = isActive
   }
    
    
}


// Initialise a User struct here

var info = User(name: "Richard", email: nil, followers: 0, isActive: false)

info.logStatus()

// Diagnostic code - do not change this code
print("\nDiagnostic code (i.e., Challenge Hint):")
var musk = User(name: "Elon", email: "elon@tesla.com", 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()

}

@alexnavter
Copy link

alexnavter commented Oct 19, 2023

// Define a structure called User with its properties
struct User {
    let name: String
    var email: String?
    var followers: Int
    var isActive: Bool

// Initializer method for the User struct. It will allow us to create a User object.
    init(name: String, email: String? = nil, followers: Int, isActive: Bool) {
        self.name = name
        self.email = email
        self.followers = followers
        self.isActive = isActive
    }

// Method defined within the User struct to check if the isActive property is true or false to print the user's status to the console
    func logStatus(){
        
        if(isActive){
            print("\(name) is working hard")
        } else {
            print("\(name) has left earth")
        }
    }
}

// Create an instance of the User struct and initialize it with its values
var richard = User(name: "Richard", email: "richard@yahoo.com", followers: 0, isActive: false)

richard.logStatus() // Output: Richard has left earth

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