Using variable/constant outside of do{}catch{} - swift2 - swift

So on a button press I'm creating splitLat: [Double] from a throwing function called splitLatitude that takes currentLocation: CLLocationCoordinate2D?. I then want to use splitLat as a Label (its going to be used for other things as well but this serves the example)
#IBAction func ButtonPress() {
let splitLat = try self.splitLatitude(self.currentLocation)
LatSplitLabel.text = "\(splitLat)"
}
this gets a error "Errors thrown from here are not handled"
I resolve this by putting it in a do catch block
do{
let splitLat = try self.splitLatitude(self.currentLocation)
} catch {
print("error") //Example - Fix
}
but the when i try to set the label later on splitLat is an "unresolved identifier"
New to swift and programming in general, am i missing something basic/ do i have a mis understanding? is there a way i can use the constant from the do {} statement outside of the do statement. Tried return but that is reserved for functions.
Really appreciate any help
Thanks

You have two options (I'll assume that splitLat is String type)
do{
let splitLat = try self.splitLatitude(self.currentLocation)
//do rest of the code here
} catch {
print("error") //Example - Fix
}
second option, predeclare the variable
let splitLat : String? //you can late init let vars from swift 1.2
do{
splitLat = try self.splitLatitude(self.currentLocation)
} catch {
print("error") //Example - Fix
}
//Here splitLat is recognized
Now, some explanation of your problem.
in Swift (and many other languages) variables are only defined inside the scope they are defined
scope is defined between these brackets {/* scope code */ }
{
var x : Int
{
//Here x is defined, it is inside the parent scope
var y : Int
}
//Here Y is not defined, it is outside it's scope
}
//here X is outside the scope, undefined

A 3rd option is to use a closure:
let splitLat:String = {
do {
return try self.splitLatitude(self.currentLocation)
}
catch {
print("error") //Example - Fix
return ""
}
}()
LatSplitLabel.text = "\(splitLat)"

This is a scoping error if you want to succeed execution after the do/catch block. You must declare the variable outside of this do/catch scope in order to utilize it after the do/catch execution.
Try this:
var splitLat: <initialType> = <initialValue>
do {
let splitLat = try self.splitLatitude(self.currentLocation)
} catch {
print("error")
}
print(splitLat)
Here is a concocted example that runs in a Swift 2.2 playground:
enum Errors: ErrorType {
case SomeBadError
}
func getResult(param: String) throws -> Bool {
if param == "" {
throw Errors.SomeBadError
}
return true
}
var result = false
do {
result = try getResult("it")
} catch {
print("Some error")
}
print(result)

Related

How to use a method with throws returning a value in promiseKit

I create a set of promises which relies on the results from a function that may throw an error. I can get this to work as shown in the code below, but I don't like the double catch blocks. I'd like to use the a single promiseKit catch block. Anyone have a better solution that works?
do {
let accounts = try Account.getAccounts()
let mailboxPromises = accounts.map { self.fetchMailboxes($0) }
when(fulfilled: mailboxPromises).map { _ in
self.updateBadgeCount()
}
.catch { (error) in
}
} catch {
}
Maybe wrap Account.getAccounts() in a promise which you can then use in your promise chain?
func getAccounts() -> Promise<[Account]> {
return Promise {
do {
let accounts = try Account.getAccounts()
$0.fulfill(accounts)
} catch {
$0.reject(error)
}
}
}
UPDATE:
Below info is from the documentation at https://github.com/mxcl/PromiseKit/blob/master/Documentation/CommonPatterns.md so you should be able to use this pattern instead of your do/catch block.
Since promises handle thrown errors, you don't have to wrap calls to throwing functions in a do block unless you really want to handle the errors locally:
foo().then { baz in
bar(baz)
}.then { result in
try doOtherThing()
}.catch { error in
// if doOtherThing() throws, we end up here
}

How to exit a function scope from inner function using Swift?

With return it is possible to exit the scope of the current function but is it also possible to exit the scope of the outer function that is calling the inner function?
func innerFunction() {
guard 1 == 2 else {
// exit scope of innerFunction and outerFunction
}
}
func outerFunction() {
innerFunction()
print("should be unreachable")
}
There could be one approach using a return value of the inner function that we can check for:
func innerFunction() -> Bool {
guard 1 == 2 else {
return false
}
return true
}
func outerFunction() {
guard innerFunction() else {
return
}
print("should be unreachable")
}
The problem with this approach is that it can clutter your code pretty quickly if the functions become more complicated and you have to use them over and over again.
Consider applying this approach with XCTest. It would look like this:
func testFoobar() {
guard let unwrappedObject = helperFunction() else {
XCTFail("A failure message that can change with each helperFunction call")
return
}
print("should be unreachable when helperFunction already failed")
}
I'd like to have something similar to this:
func testFoobar() {
let unwrappedObject = helperFunction()
print("should be unreachable when helperFunction already failed")
}
This is basically what Swift's error handling does:
func outer() throws
{
try inner()
print("Unreachable")
}
struct DoNotContinue : Error {}
func inner() throws
{
throw DoNotContinue()
}
do { try outer() }
catch _ { print("Skipped the unreachable") }
Note, of course, that the caller still has control over this: it could catch the thrown error itself instead of just exiting.
problem with this approach is that it can clutter your code
There's a much more serious problem with allowing callees to directly exit their callers, and that is that the flow of control very quickly becomes incomprehensible. Imagine that you have a couple of layers of this. Reading the top-level function, you no longer have any clear idea what can happen. You must yourself recurse into every callee's callee to make sure that the original code will continue on its course.

Function with throws doesn't exit the function with executes trow

I have the following function with throws:
func doSomething(_ myArray: [Int]) throws -> Int {
if myArray.count == 0 {
throw arrayErrors.empty
}
return myArray[0]
}
But when the array is equal to 0 it will throw the error but it will continue executing the function returning empty array.
How can I exit the function when goes to the if statement ?
The function does return when you throw an error. Pop this in a playground and look at the output.
//: Playground - noun: a place where people can play
enum MyError: Error {
case emptyArray
}
func doSomething<T>(_ array: [T]) throws -> T {
guard !array.isEmpty else {
throw MyError.emptyArray
}
return array[0]
}
do {
let emptyArray: [Int] = []
let x = try doSomething(emptyArray)
print("x from emptyArray is \(x)")
} catch {
print("error calling doSeomthing on emptyArray: \(error)")
}
do {
let populatedArray = [1]
let x = try doSomething(populatedArray)
print("x from populatedArray is \(x)")
} catch {
print("error calling doSeomthing on emptyArray: \(error)")
}
You'll see the output is
error calling doSeomthing on emptyArray: emptyArray
x from populatedArray is 1
Note that you don't see the output from print("x from emptyArray is \(x)") because it was never called, because throw ends the execution of that function. You can also confirm this because guard statements require the function to exit.
Also, just to note, if you want the first thing from an array you can just use myArray.first which will return T?, and you can handle the nil case instead of having to deal with an error. For example:
//: Playground - noun: a place where people can play
let myArray = [1, 2, 3]
if let firstItem = myArray.first {
print("the first item in myArray is \(firstItem)")
} else {
print("myArray is empty")
}
let myEmptyArray: [Int] = []
if let firstItem = myEmptyArray.first {
print("the first item in myEmptyArray is \(firstItem)")
} else {
print("myEmptyArray is empty")
}
which outputs:
the first item in myArray is 1
myEmptyArray is empty
You need to understand error handling. If you insert a throws keyword, anybody calling it will need to use a do-catch statement, try?, try! or continue to propagate them. So what will happen if the error is thrown is up to the caller.
Here's an example:
do {
try doSomething(foo)
} catch arrayErrors.empty {
fatalError("Array is empty!")
}
Apple's documentation for error handling
If you want to exit once you reach the if, simply don't use error handling and call fatalError inside the if.
if myArray.count = 0 {
fatalError("Error happened")
}

Can I call a swift function that throws in an if statement?

Say I have a function called:
func myFunction() throws -> Bool {
// ...
return true
}
Is there a syntax I am unaware of to use it like so:
if aFlag && try myFunction() {
// do this
}
When I compile this, it fails of course because it says a "try cannot appear to the right of a non-assignment operation". Is there a way to make this work or will I have to go with my current solution of:
if aFlag {
let result = try myFunction()
if result {
// do this
}
}
You use try? to handle an error by converting it to an optional value, and combine with the nil coalescing operator to supply e.g. false as concrete value in case the call throws (-> treated as nil)
func myFunction() throws -> Bool {
return true
}
let aFlag = true
if aFlag, (try? myFunction()) ?? false {
print("Function call succeeded and returned 'true'!")
}
/* or ...
if aFlag && (try? myFunction()) ?? false { ... } */
/* or, optionally bind the return value and use as predicate if non-nil ...
if aFlag, let anotherFlag = (try? myFunction()), anotherFlag { ... } */
This will, however, supress the error thrown, which makes it kind of questionable for myFunction() to be a throwing one in the first place. Perhaps you rather want a function that simply returns nil in the cases you treat as erroneous in your throwing function?
In Swift 3, you can use , rather than &&, e.g.
if aFlag, try myFunction() {
print("OK")
}

Swift: guard let vs if let

I have been reading about Optionals in Swift, and I have seen examples where if let is used to check if an Optional holds a value, and in case it does – do something with the unwrapped value.
However, I have seen that in Swift 2.0 the keyword guard let is used mostly. I wonder whether if let has been removed from Swift 2.0 or if it still possible to be used.
Should I change my programs that contain if let to guard let?
if let and guard let serve similar, but distinct purposes.
The "else" case of guard must exit the current scope. Generally that means it must call return or abort the program. guard is used to provide early return without requiring nesting of the rest of the function.
if let nests its scope, and does not require anything special of it. It can return or not.
In general, if the if-let block was going to be the rest of the function, or its else clause would have a return or abort in it, then you should be using guard instead. This often means (at least in my experience), when in doubt, guard is usually the better answer. But there are plenty of situations where if let still is appropriate.
Guard can improve clarity
When you use guard you have a much higher expectancy for the guard to succeed and it's somewhat important that if it doesn't succeed, then you just want to exit scope early. Like you guard to see if a file/image exists, if an array isEmpty or not.
func icon() -> UIImage {
guard let image = UIImage(named: "Photo") else {
return UIImage(named: "Default")! //This is your fallback
}
return image //-----------------you're always expecting/hoping this to happen
}
If you write the above code with if-let it conveys to the reading developer that it's more of a 50-50. But if you use guard you add clarity to your code and it implies I expect this to work 95% of the time...if it ever failed, I don't know why it would; it's very unlikely...but then just use this default image instead or perhaps just assert with a meaningful message describing what went wrong!
Avoid guards when they create side effects, guards are to be used as a natural flow. Avoid guards when else clauses introduce side effects.
Guards establish required conditions for code to execute properly,
offering early exit
When you perform significant computation in the positive branch, refactor from if to a guard statement and returns the fallback value
in the else clause
From: Erica Sadun's Swift Style book
Also as a result of the above suggestions and clean code, it's more likely you will want/need to add assertions into failed guard statements, it just improves readability and makes it clear to other developers what you were expecting.
guard​ ​let​ image = ​UIImage​(named: selectedImageName) else { // YESSSSSS
assertionFailure(​"Missing ​​\(​selectedImageName​)​​ asset"​)
return
}
guard​ ​let​ image = ​UIImage​(named: selectedImageName) else { // NOOOOOOO
​  ​return
}
From: Erica Sadun's Swift Style book + some modifications
(you won't use asserts/preconditions for if-lets. It just doesn't seem right)
Using guards also help you improve clarity by avoiding pyramid of doom. See Nitin's answer.
Guard keeps code that handles a violated requirement next to the requirement
To be clear, guard isn't always about success vs failure. The more generic way to see it is about handling a violated requirement vs process code that isn't violated.
example:
func getImage(completion: (image: Image)? -> Void) {
guard cache["image1"] == false else {
completion(cache["image1"]!)
}
downloadAndStore("image1") { image in
completion(image)
}
}
In the above the requirement is for the image to not be present in cache. If the image is present then our requirement is violated. We return early. As you can see we also handle the violated code path, right next to its requirement i.e. the structure is not:
if requirement {
.
.
ten lines of code
.
.
} else {
handle requirement
}
The Swift Docs on Control Flow explain the idea behind that:
Using a guard statement for requirements improves the readability of
your code, compared to doing the same check with an if statement.
It lets you write the code that’s typically executed without wrapping it in an else block
it lets you keep the code that handles a violated requirement next to the requirement.
Guard avoids nesting by creating a new variable in the current scope
There is one important difference that I believe no one has explained well.
Both guard let and if let unwrap the variable however
With guard let you are creating a new variable that will exist in the current scope.
With if let you’re only creating a new variable inside the code block.
guard let:
func someFunc(blog: String?) {
guard let blogName = blog else {
print("some ErrorMessage")
print(blogName) // will create an error Because blogName isn't defined yet
return
}
print(blogName) // You can access it here ie AFTER the guard statement!!
//And if I decided to do 'another' guard let with the same name ie 'blogName' then I would create an error!
guard let blogName = blog else { // errorLine: Definition Conflicts with previous value.
print(" Some errorMessage")
return
}
print(blogName)
}
if-let:
func someFunc(blog: String?) {
if let blogName1 = blog {
print(blogName1) // You can only access it inside the code block. Outside code block it doesn't exist!
}
if let blogName1 = blog { // No Error at this line! Because blogName only exists inside the code block ie {}
print(blogName1)
}
}
For more info on if let do see: Why redeclaration of optional binding doesn't create an error
Guard requires scope exiting
(Also mentioned in Rob Napier's answer) :
You MUST have guard defined inside a func. It's major purpose is to abort/return/exit scope, if a condition isn't met:
var str : String?
guard let blogName1 = str else {
print("some error")
return // Error: Return invalid outside of a func
}
print (blogName1)
For if let you don't need to have it inside any func:
var str : String?
if let blogName1 = str {
print(blogName1) // You don't get any errors!
}
guard vs if
It's worth noting that it's more appropriate to see this question as guard let vs if let and guard vs if.
A standalone if doesn't do any unwrapping, neither does a standalone guard. See example below. It doesn't exit early if a value is nil. There are NO optional values. It just exits early if a condition isn't met.
let array = ["a", "b", "c"]
func subscript(at index: Int) -> String?{
guard index > 0, index < array.count else { return nil} // exit early with bad index
return array[index]
}
When to use if-let and when to use guard is often a question of style.
Say you have func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int and an optional array of items (var optionalArray: [SomeType]?), and you need to return either 0 if the array is nil (not-set) or the count if the array has a value (is set).
You could implement it like this using if-let:
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
if let array = optionalArray {
return array.count
}
return 0
}
or like this using guard:
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
guard let array = optionalArray else {
return 0
}
return array.count
}
The examples are functionally identical.
Where guard really shines is when you have a task like validating data, and you want the function to fail early if anything is wrong.
Instead of nesting a bunch of if-lets as you get closer to finishing validation, the "success path" and the now successfully bound optionals are all in the main scope of the method, because the failure paths have all returned already.
I'll try to explain the usefulness of guard statements with some (unoptimized) code.
You have a UI where you are validating text fields for user registration with first name, last name, email, phone and password.
If any textField is not containing valid text, it should make that field firstResponder.
here is the unoptimized code:
//pyramid of doom
func validateFieldsAndContinueRegistration() {
if let firstNameString = firstName.text where firstNameString.characters.count > 0{
if let lastNameString = lastName.text where lastNameString.characters.count > 0{
if let emailString = email.text where emailString.characters.count > 3 && emailString.containsString("#") && emailString.containsString(".") {
if let passwordString = password.text where passwordString.characters.count > 7{
// all text fields have valid text
let accountModel = AccountModel()
accountModel.firstName = firstNameString
accountModel.lastName = lastNameString
accountModel.email = emailString
accountModel.password = passwordString
APIHandler.sharedInstance.registerUser(accountModel)
} else {
password.becomeFirstResponder()
}
} else {
email.becomeFirstResponder()
}
} else {
lastName.becomeFirstResponder()
}
} else {
firstName.becomeFirstResponder()
}
}
You can see above, that all the strings (firstNameString, lastNameString etc) are accessible only within the scope of the if statement. so it creates this "pyramid of doom" and has many issues with it, including readability and ease of moving things around (if the fields' order is altered, you have to rewrite most of this code)
With the guard statement (in the code below), you can see that these strings are available outside the {} and are made use of, if all fields are valid.
// guard let no pyramid of doom
func validateFieldsAndContinueRegistration() {
guard let firstNameString = firstName.text where firstNameString.characters.count > 0 else {
firstName.becomeFirstResponder()
return
}
guard let lastNameString = lastName.text where lastNameString.characters.count > 0 else {
lastName.becomeFirstResponder()
return
}
guard let emailString = email.text where
emailString.characters.count > 3 &&
emailString.containsString("#") &&
emailString.containsString(".") else {
email.becomeFirstResponder()
return
}
guard let passwordString = password.text where passwordString.characters.count > 7 else {
password.becomeFirstResponder()
return
}
// all text fields have valid text
let accountModel = AccountModel()
accountModel.firstName = firstNameString
accountModel.lastName = lastNameString
accountModel.email = emailString
accountModel.password = passwordString
APIHandler.sharedInstance.registerUser(accountModel)
}
If the order of the fields changes, just move respective lines of code up or down, and you are good to go.
This is a very simple explanation and a use case. Hope this helps!
Basic Difference
Guard let
Early exist process from the scope
Require scope existing like return, throw etc.
Create a new variable that can be accessed outside the scope.
if let
Can not access outside the scope.
no need for return statement. But we can write
NOTE: Both are used to unwrap the Optional variable.
guard let vs if let
func anyValue(_ value:String?) -> String {
guard let string = value else {
return ""
}
return string
}
func anyValue(_ value:String?) -> String {
if let string = value {
return string
}
return ""
}
The clearest explanation I saw was in the Github Swift Style Guide:
if adds a level of depth:
if n.isNumber {
// Use n here
} else {
return
}
guard doesn't:
guard n.isNumber else {
return
}
// Use n here
guard
A guard statement is used to transfer program control out of a scope
if one or more conditions aren’t met.
The value of any condition in a guard statement must be of type Bool
or a type bridged to Bool. The condition can also be an optional
binding declaration.
A guard statement has the following form:
guard condition else {
//Generally return
}
if let
Also popular as optional binding.
For accessing optional object we use if let.
if let roomCount = optionalValue {
print("roomCount available")
} else {
print("roomCount is nil")
}
I learnt this from swift with Bob..
Typical Else-If
func checkDrinkingAge() {
let canDrink = true
if canDrink {
print("You may enter")
// More Code
// More Code
// More Code
} else {
// More Code
// More Code
// More Code
print("Let me take you to the jail")
}
}
Issues with Else-If
Nested brackets
Have to read every line to spot the error message
Guard Statement
A guard block only runs if the condition is false, and it will exit out of the function through return. If the condition is true, Swift ignores the guard block. It provides an early exit and fewer brackets.+
func checkDrinkProgram() {
let iCanDrink = true
guard iCanDrink else {
// if iCanDrink == false, run this block
print("Let's me take you to the jail")
return
}
print("You may drink")
// You may move on
// Come on.
// You may leave
// You don't need to read this.
// Only one bracket on the bottom: feeling zen.
}
Unwrap Optionals with Else-If
A guard statement is not only useful for replacing a typical conditional block with an else-if statement, but also great for unwrapping optionals by minimizing the number of brackets. To compare, let's first begin how to unwrap multiple optionals with else-if.
First, let us create three optionals that will be unwrapped.
var publicName: String? = "Bob Lee"
var publicPhoto: String? = "Bob's Face"
var publicAge: Int? = nil
The Worst Nightmare
func unwrapOneByOne() {
if let name = publicName {
if let photo = publicPhoto {
if let age = publicAge {
print("Bob: \(name), \(photo), \(age)")
} else {
print("age is mising")
}
} else {
print("photo is missing")
}
} else {
print("name is missing")
}
}
The code above certainly works but violates the DRY principle. It's atrocious. Let us break it down.+
Slightly Better
The code below is more readable than above.+
func unwrapBetter() {
if let name = publicName {
print("Yes name")
} else {
print("No name")
return
}
if let photo = publicPhoto {
print("Yes photo")
} else {
print("No photo")
return
}
if let age = publicAge {
print("Yes age")
} else {
print("No age")
return
}
}
Unwrap with Guard
The else-if statements can be replaced with guard.+
func unwrapOneByOneWithGuard() {
guard let name = publicName else {
print("Name missing")
return
}
guard let photo = publicPhoto else {
print("Photo missing")
return
}
guard let age = publicAge else {
print("Age missing")
return
}
print(name)
print(photo)
print(age)
}
Unwrap Multiple Optionals with Else-If
So far, you've been unwrapping optionals one by one. Swift allows us to unwrap multiple optionals at once. If one of them contains nil, it will execute the else block.
func unwrap() {
if let name = publicName, let photo = publicPhoto, let age = publicAge {
print("Your name is \(name). I see your face right here, \(photo), you are \(age)")
} else {
// if any one of those is missing
print("Something is missing")
}
}
Be aware that when you unwrap multiple optionals at once, you can't identify which contains nil
Unwrap Multiple Optionals with Guard
Of course, we should use guard over else-if.+
func unwrapWithGuard() {
guard let name = publicName, let photo = publicPhoto, let age = publicAge else {
// if one or two of the variables contain "nil"
print("Something is missing")
return
}
print("Your name is \(name). I see your, \(photo). You are \(age).")
// Animation Logic
// Networking
// More Code, but still zen
}
The main difference between guard and if statements in swift are:
The if statement is used to run code when a condition is met.
The guard statement is used to run code when a condition is not met.