Skip to content

Instantly share code, notes, and snippets.

@bobleesj
Last active January 26, 2017 15:27
Show Gist options
  • Save bobleesj/37887869ce417b1e70fcedd3b0190d54 to your computer and use it in GitHub Desktop.
Save bobleesj/37887869ce417b1e70fcedd3b0190d54 to your computer and use it in GitHub Desktop.
// 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”)
}
@funky-monkey
Copy link

Better to check isEmpty on name instead of name == "". Or better yet use guard to guard that name is not empty.
Like this:

init(name: String) throws {
    guard !name.isEmpty else { throw CourseError.noName }
    self.courseName = name
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment