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 trial

iOS

m daymon
m daymon
1,091 Points

SWIFT - 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
Chris Shaw
26,676 Points

Hi,

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.

  1. 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.e Member which is standard convention.

  2. It's redundant to have an init method unless you're using it, you can safely remove it.

  3. Swift has a shortcut for creating arrays which is the square [] brackets, i.e Array<Member> can change to [Member] which implies the same type of Array.

  4. 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
m daymon
1,091 Points

thanks.. 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