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!
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 trial

m daymon
1,091 PointsSWIFT - Array of Custom Objects
I have the following code. I can't understand why when I loop through the array in the "call" method I don't see the data.
You can drop the code below into a playground and see exactly what I mean..
func findMembershipsForUser(user:String)->Array<Members>{
var res1 = Members()
res1.memberName = "approved"
var res2 = res1
res2.memberName = "notapproved"
var newArray = [Members]()
newArray.append(res1)
newArray = [res1,res2]
newArray
return newArray
}
func call(){
var result = findMembershipsForUser("joe")
var firstObject = result[0]
firstObject.memberName!
for member in result{
member.memberName! //Not working
}
}
call()
1 Answer

Chris Shaw
26,663 PointsHi,
The code you have above shouldn't execute at all because the keyword Class
is not valid as the C needs to be lowercase, also here's some other things I've noted down.
Class names should describe what they're for, in your case you've created a class called
Members
in the plural tense but instead you want it to be singular i.eMember
which is standard convention.It's redundant to have an
init
method unless you're using it, you can safely remove it.Swift has a shortcut for creating arrays which is the square
[]
brackets, i.eArray<Member>
can change to[Member]
which implies the same type ofArray
.You're currently cloning your
res1
variable which you don't want to do as classes when cloned act as references which means when you updated the clone(s) it also updates the original instance of the class, it's best practice to create a new instance of the class to avoid overwriting your data.
Moving on!
Below I've fixed and altered your code to behave in the way you expect, all of the above are included as part of the changes but I've also updated the Member
class constructor to accept name: String
as an parameter to reduce clutter along with placing the array definition on the return statement itself which cleans the code up more.
Aside from that it's essentially the same code from there on out.
class Member {
var memberName: String?
init(name: String) {
memberName = name
}
}
func findMembershipsForUser() -> [Member] {
let res1 = Member(name: "approved")
let res2 = Member(name: "notapproved")
return [res1, res2]
}
let members = findMembershipsForUser()
println(members[0].memberName!)
println(members[1].memberName!)
If that's confusing let me know but hopefully that helps, happy coding.
m daymon
1,091 Pointsm daymon
1,091 Pointsthanks.. yes I can see the error/issue clearly now. part of the reason I thought there as a bigger issue as that I wasn't looking at the console output in the for loop