Skip to content

Instantly share code, notes, and snippets.

@agoiabel
Last active April 16, 2020 20:13
Show Gist options
  • Save agoiabel/bdd898d752f28dfaee38771c3c981f9a to your computer and use it in GitHub Desktop.
Save agoiabel/bdd898d752f28dfaee38771c3c981f9a to your computer and use it in GitHub Desktop.
Person {
var firstname: String
var middlename: String
var lastname: String
}
//this will definitely work because i have a middlename
let me = Person(firstname: "Abel", middlename: "abel", lastname: "Adeyemi")
//This will throw an error
let anotherPerson = Person(firstname: "Abel", middlename: nil, lastname: "Adeyemi")
@SrivalliChaturvedula
Copy link

I'm getting error executing the same code. Please let me know the solution

@agoiabel
Copy link
Author

What’s the error message

@SrivalliChaturvedula
Copy link

Screen Shot 2020-04-16 at 8 42 59 AM

@agoiabel
Copy link
Author

agoiabel commented Apr 16, 2020

You should have it in a Struct or Class.

import UIKit;

struct Person {
  var firstname: String
  var middlename: String? //we created the optional with the ?
  var lastname: String
  
  func fullName() -> () {
     print("Your fullname is \(firstname) \(middlename!) \(lastname)")
  }
}

let me = Person(firstname: "Abel", middlename: "Agoi", lastname: "Adeyemi");

me.fullName();

@SrivalliChaturvedula
Copy link

Thankyou

@SrivalliChaturvedula
Copy link

It shows me error if I use the class . Send me code using class keyword.

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