Created
June 12, 2015 12:29
-
-
Save konrad1977/2eb3e5928ae746746731 to your computer and use it in GitHub Desktop.
try catch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| enum CustomError : ErrorType { | |
| case ErrorWithMessage(message: String) | |
| } | |
| func loginUserWithName(username: String?) throws -> String { | |
| guard let username = username where username.characters.count != 0 else { | |
| throw CustomError.ErrorWithMessage(message: "No username provided") | |
| } | |
| return "token:" + username | |
| } | |
| func login() { | |
| do { | |
| let token = try loginUserWithName(nil) | |
| print("user logged in \(token)") | |
| } catch { | |
| print("An error occurred \(error)") | |
| } | |
| } | |
| login() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment