Start with the following code:
class Person {
let firstName: String
let lastName: String
let age: Int
init(firstName: String, lastName: String, age: Int) {
self.firstName = firstName
self.lastName = lastName
self.age = age
}
}
let james = Person(firstName: "James", lastName: "Pacheco", age: 26)
let andrea = Person(firstName: "Andrea", lastName: "Mower", age: 24)
let carol = Person(firstName: "Rebecca", lastName: "Mordo", age: 30)
let clubMembers: [Person] = [james, andrea, carol]
Person
objects. Add one of them to the club array, but do not add the other.areYouAMemberOfTheClub
that takes a Person
object as a parameter and returns a Bool
indicating whether or not the person passed in is a member of the club. It should return true for the person that you added to the array of club members and false for the other person.
clubMembers
array as a reference.clubMembers
array in your function and see if the person passed in is equal to any of them using person == clubMembers[i]
, or something along those lines. Notice that you cannot use the ==
operator. This is because you must conform to the Equatable protocol. Go look it up in documentation.Person
object conform to the Equatable protocol and outside of the Person
class create the necessary Equatable function. You should now be able to use the ==
operator between two Person
objects, making it easier to make your areYouAMemberOfTheClub
function. By conforming to the Equatable protocol, you are also given some array methods that help solve this problem (you don't have to use them, but they make your life easier).Create a new X-Code Project called ClubMembers. Copy over your Person
class, clubMembers
array, and make a new array that contains all of your Person
objects, whether club members or not. Make sure to copy over your Equatable function as well (all of these should be put into the proper files, which you'll need to make). Then create a tableView that displays all people with a subtitle that says club member or not a club member. Add a button that will reload your tableView and only show club members.