Last active
January 26, 2017 15:27
-
-
Save bobleesj/37887869ce417b1e70fcedd3b0190d54 to your computer and use it in GitHub Desktop.
This file contains 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
// Define Error Type | |
enum CourseError: Error { | |
case noName | |
} | |
// Create Structure | |
struct UdemyCourse { | |
let courseName: String | |
init(name: String) throws { | |
if name == “” { throw CourseError.noName } | |
self.courseName = name | |
} | |
} | |
// Init & Handle Error | |
do { | |
try UdemyCourse(name: “UIKit Fundamentals with Bob”) | |
} catch NameError.noName { | |
print(“Bob, you need to enter the name”) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Better to check
isEmpty
onname
instead ofname == ""
. Or better yet useguard
to guard thatname
is not empty.Like this: