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 trialJohnathan Maravilla
10,680 PointsDeclaring a new variable referencing a struct stored property variable (Stuct vs Class | Value Type vs Reference Type)
In the Beginning iOS::Object-Oriented Swift::Structs vs Classes video starting at 3:04, Pasan states that a newly declared variable that references a struct stored property variable would be ignored and the original stored property would result.
This is not the case when I attempted to reproduce his statement. Is this a new feature within Swift 4?
Example:
struct User {
var fullName: String
var email: String
var age: Int
}
var someUser = User(fullName: "Johnathan Maravilla", email: "fake@metastry.com", age: 33)
var anotherUser = someUser
anotherUser.email
someUser.email = "test@metastry.com"
print(someUser.email)
1 Answer
Johnathan Maravilla
10,680 PointsI figured it out. My code was incorrect:
someUser.email = "test@metastry.com"
This line of code was outside of bounds and should have been under
anotherUser.email
I was also calling the wrong instance when attempting to print and the result was as it should have been based upon what I was calling.
Side note: I found a blog post by Apple, explaining the differences between value types and reference types. it helped me understand the difference a bit more.