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 Error Handling in Swift 2.0 Error Handling Handling Errors

Kevin Gutowski
Kevin Gutowski
4,082 Points

Xcode won't compile code fully (8.0, 8S128d)

For some reason Xcode will just hang when trying to run my code.

struct Friend {
    let name: String
    let age: String
    let address: String?
}

enum FriendError: ErrorProtocol {
    case InvalidData(String)
}

func createFriendFromJSON(dict: [String : String]) throws -> Friend {

    guard let name = dict["name"] else {
        throw FriendError.InvalidData("Name key fails")
    }

    guard let age = dict["age"] else {
        throw FriendError.InvalidData("Age key fails")
    }

    let address = dict["address"]

    return Friend(name: name, age: age, address: address)
}

func sendMessageToFriend(friend: Friend, message: String) {}

let response = ["name": "Pasan", "ages": "27", "address": "someAddress"]

do {
    let friend = try createFriendFromJSON(dict: response)
    sendMessageToFriend(friend: friend, message: "")
} catch FriendError.InvalidData(let key) {
    print(key)
}

I just keep getting the spinner as if Xcode is trying to compile but isn't completing. Any ideas?

Kevin Gutowski
Kevin Gutowski
4,082 Points

I'm not getting any complier errors. In fact, I don't have any errors at all. It just hangs.

2 Answers

enum FriendError: ErrorProtocol { case InvalidData(String) }

Change ErrorProtocol to ErrorType

I hope this helps you!

Kevin Gutowski
Kevin Gutowski
4,082 Points

Hi Trent! ErrorType is changed to ErrorProtocol in the new version of Swift (3.0).

Example: http://swiftdoc.org/v3.0/protocol/ErrorProtocol/

were not on swift 3 right now

Chris Lopez
Chris Lopez
8,155 Points

ErrorType is now Error in the latest version of swift 3