Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialNicholas Moore
1,117 PointsI don't understand what a "struct" is or does
I watched the video twice and don't understand what the "struct" does. And what are objects and instances? Instance methods?
struct Person {
let firstName: String
let lastName: String
func fullName(first: String, last: String) -> (String) {
var name = Person("first ", "last")
return name
}
}
Nicholas Moore
1,117 PointsOk, so then could you tell me what is wrong with my code in the challenge?
2 Answers
Stipe Vucemilovic
iOS Development Techdegree Student 9,138 Pointsstruct Person {
let firstName: String
let lastName: String
func fullName() -> String {
return "\(firstName) \(lastName)"
}
}
let someName = Person(firstName: "FirstName", lastName: "LastName")
someName.fullName()
Stipe Vucemilovic
iOS Development Techdegree Student 9,138 PointsHello Nicholas,you can try something like this..
struct Person { let firstName: String let lastName: String
func fullName() -> String {
return "\(firstName) \(lastName)"
}
}
let someName = Person(firstName: "FirstName", lastName: "LastName") someName.fullName()
Struct is an object, and unlike classes you don't need to write it s init method,structs has memberwise initializer so you can right away assign instance of struct in this case to constant someName and just call method fullName on that instance(fullName is instance method and you can get full name by returning string with string interpolation)
Hope this helps..
Kirby Ziada
9,886 PointsKirby Ziada
9,886 PointsA 'struct' is the building block to your code, use a 'struct' over a 'class' if it's more simplistic. The reason being a 'class' can inherit from other 'classes', a 'struct' can't.
• Instances are basically variables/constants in your struct
• Instance methods are functions within the struct
You can find more information here on Methods (https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Methods.html) Structs and Classes (https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html)