-
-
Save TheMuellenator/569463645ec81ea49f85ebf6d6dd99fe to your computer and use it in GitHub Desktop.
// 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() | |
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.
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")
}
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()
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()
}
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()
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()
}
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()
}
// 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
func exercise() {
// Define the User struct here
struct User {
var name : String
var email : String?
var followers : Int
var isActive : Bool
func logStatus(){
if self.isActive == true {
print("\(self.name) is working hard")
}
else {
print("\(self.name) has left earth")
}
}
}
// Initialise a User struct here
var userRichard = User(name: "Richard",email: nil, followers: 0, isActive: false)
userRichard.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()
}
func exercise() {
print("Richard has left earth")
print("\nDiagnostic code (i.e., Challenge Hint):\nElon is working hard\nContacting Elon on elon@tesla.com ...\nElon has 2001 followers\nElon has left earth")
}
That's the only thing that worked out right. ☝️
Because this:
func exercise() {
// Define the User struct here
struct User {
let name: String
var email: String?
var followers: Int
var isActive: Bool
init(name: String, email: String? = nil, followers: Int, isActive: Bool) {
self.name = name
self.email = email
self.followers = followers
self.isActive = isActive
}
func logStatus(){
if self.isActive {
print("\(self.name) is workin hard")
} else {
print("\(self.name) has left earth")
}
}
}
var gere = User(name: "Richard", followers: 0, isActive: false)
gere.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()
}
returned the following to me:👇
Error details
/eval/Tests/BaseTests/Evaluate.swift:9: error: Evaluate.testExample : XCTAssertEqual failed: ("Richard has left earth
Diagnostic code (i.e., Challenge Hint):
Elon is workin hard
Contacting Elon on elon@tesla.com ...
Elon has 2001 followers
Elon has left earth
") is not equal to ("Richard has left earth
Diagnostic code (i.e., Challenge Hint):
Elon is working hard
Contacting Elon on elon@tesla.com ...
Elon has 2001 followers
Elon has left earth
") - You should print 'Hello World'
Coding Exercise
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: "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()
}
// Don't modify this code
exerciseStruct()
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: "elon@tesla.com", 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.
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: "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()
}
func exercise() {
}