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 Declaring a Protocol

Chase Maddox
PLUS
Chase Maddox
Courses Plus Student 5,461 Points

Beginning Protocol Code Challenge #1

Hi,

Why is the Code Challenge not accepting my answer? It gives the error, "Make sure User specifies a gettable property named name of type String", but I thought I did specify a gettable property named 'name'.

Thanks, Chase

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

3 Answers

Mika Kiela
Mika Kiela
2,178 Points

Hi Chase!

You should just use spaces around the gettable and settable. Like this:

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

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

doesn't pass, why?

Pasan Premaratne

In the first challenge we are asked to make make a user protocol, in doing so the following fails:

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

even though it does work in XCODE

This passes the challenge:

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

I think it is too strict, if XCode has no problem with it why should it matter? Also the hints don't point out to the fact that it is a syntax problem.

Mika Kiela
Mika Kiela
2,178 Points

doesn't pass, why?

The Colons after the variables, must be right after 'name' and 'age'. So there is two extra spaces over there.

Example:

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