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 Protocols in Swift Protocol Basics Conforming to a Protocol

In the editor the protocol you created, User, is provided along with an empty struct. For this task, Person needs to con

In the editor the protocol you created, User, is provided along with an empty struct. For this task, Person needs to conform to User. After you've written code to achieve this task, create an instance of Person and assign it to a constant named somePerson.

protocols.swift
protocol UserType {
  var name: String { get }
  var age: Int { get set }
}

struct Person: UserType {
      var name: String
      var age: String
init(name: String, age: Int) {
    self.name = name
    self.age = age
  }
}

let somePerson = Person(name: "Billy", age: 22)

2 Answers

Hi Alexisca, Structs have automatic member wise init's, so we don't need to write one ourselves. Also be careful with object names/spelling, the TreeHouse Workspaces compiler is very fussy! Make sure you use the names you are given & those requested by the challenge:

protocol User {
  var name: String { get }
  var age: Int { get set }
}
struct Person: User {
      var name: String
      var age: String
}

let somePerson = Person(name: "Billy", age: 22)

[MOD: edited code block - srh]

Remember that age is asked to be Int and not String as you've declare it on the struct bro!

oh yea ha thanks