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

Paul Je
Paul Je
4,435 Points

How Do You Declare Different Types to conform a struct into a Protocol?

Unsure of the syntax I'd need to declare them when I'd need to declare the overall type of the struct as both an Int and a String; the example prior only spoke of one where it would be a string. Therefore I don't know how to structure the return statement to include two different types of return values, any help would be appreciated!

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

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

    var DatGuyOrGirl: UserType {
        return "\(name) \(age)"

    }
}
    let somePerson = Person(name: "Paul Je", age: 22)

2 Answers

Reed Carson
Reed Carson
8,306 Points

no bug, this worked for me

struct Person: UserType {
    var name: String
    var age: Int

}

let somePerson = Person(name: "Reed", age: 28)

its probably because you threw in that extra var there. It didnt ask for that

I think there is a bug in this exercise.

Example:

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

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

let somePerson = Person(name: "Tyler" age: 100)

Above I expect the following error: swift_lint.swift:13:39: error: expected ',' separator let somePerson = Person(name: "Tyler" age: 100)

Make modification protocol UserType { var name: String { get } var age: Int { get set } }

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

let somePerson = Person(name: "Tyler" , age: 100)

When I click Preview I no longer show errors in the output.html file

Next I click on Recheck work and I receive the following error below the challenge description: Bummer! Make sure that Person conforms to the UserType protocol