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

Beginning Protocol code challenge #2

Can you guys help me with this? the problem is when I'm getting to "var somePerson: String and Int" and I don't know how should I say it.

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

struct Person {
   let name: String
   let age: Int

   var somePerson: String, Int {
      return "\(name) \(age)"
   }
}

let somePerson = Person(name: "Batuhan", age: 19)
Person.somePerson

2 Answers

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

Hey there!

almost there! The person struct in your code did not add the protocol conformance. Moreover the somePerson variable is invalid syntax, type should be String for what you want to achieve. However, this is not needed with this challenge. Also beware, although you can print out variables in Playgrounds like this: Person.somePerson, this is actually invalid on normal code, as it provides no functionality at all. The following should work:

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

// Struct Person has to conform to protocol User
// The protocol a class or struct adheres to is defined
// with a colon as here
struct Person: User {
   let name: String
   let age: Int
}

let somePerson = Person(name: "Batuhan", age: 19)

Hope that helps :)

thanks sir

I think I'm still doing something wrong.

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

struct Person { let name: String let age: Int }

let somePerson = Person(name: "Batuhan", age: 19)

It still doesn't work can you help me?

Dean Kreseski
Dean Kreseski
2,418 Points

If you change the two properties inside of the struct to variables instead of let constants it will work. hope this helps.