try, try! & try? what’s the difference, and when to use each?

Updated for Swift 5.1 Assume the following throwing function: enum ThrowableError: Error { case badError(howBad: Int) } func doSomething(everythingIsFine: Bool = false) throws -> String { if everythingIsFine { return “Everything is ok” } else { throw ThrowableError.badError(howBad: 4) } } try You have 2 options when you try calling a function that may throw. … Read more

Swift’s guard keyword

Reading this article I noticed great benefits using Guard Here you can compare the use of guard with an example: This is the part without guard: func fooBinding(x: Int?) { if let x = x where x > 0 { // Do stuff with x x.description } // Value requirements not met, do something } … Read more

tech