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 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