Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.