Questions tagged [guard-statement]

A guard statement is used to transfer program control out of a scope if one or more conditions aren’t met.

guard is a conditional statement that requires execution to exit the current block if the condition isn’t met.

Syntax:

guard <condition> else {
    <statements>
}

The value of any condition in a guard statement must have a type that conforms to the BooleanType protocol, it can also be an optional binding declaration.

Any constants or variables assigned a value from an optional binding declaration in a guard statement condition can be used for the rest of the guard statement’s enclosing scope.

For example:

guard let value = someOptional else {
     // someOptional is nil, value is not accessible
     return
}

// do something with the value
// value is available here
42 questions
0
votes
2 answers

How to use let with guards in Haskell?

I'm trying to do the following in Haskell: someFunction :: [] -> Int someFunction list let a | length list == 1 = 10 | otherwise = 0 b | length list == 1 = 10 | otherwise = 0 in findValues (a+b) So that the values of a and b will be…
0
votes
2 answers

(Swift) Call function in guard statement

I am trying to call a function called 'nextPage' in a guard statement, but it is saying '()' is not convertible to 'Bool'. What do I need to do in order to call this function @IBAction func nextPressed(_ sender: Any) { let geoCoder =…
Niall Kiddle
  • 1,477
  • 1
  • 16
  • 35
0
votes
1 answer

Proper Use of the Swift Guard Keyword?

I've been looking up how to use the guard keyword in Swift. Recently a developer told me that the code below will print "success" if there's no error in the closure. for attachment in attachments! { attachment.fetchData { (data, error) in …
0
votes
0 answers

Guard Statement Parameter Error

In hopes of writing more readable code, I am trying to employ guard statements where applicable to check for the right conditions first. However, I am encountering compile time errors. Here is my code: //guard statements //guards provide early exits…
Govind Rai
  • 14,406
  • 9
  • 72
  • 83
0
votes
1 answer

Swift 2 optional guard on string

I'm dealing with an issue in swift 2.0 I get a json file from an API and i'm trying to unwrap some strings out of it. Sometimes this json gives me a string with the street name of a venue but sometimes not. So when i'm trying this var street =…
Konstantinos Natsios
  • 2,874
  • 9
  • 39
  • 74
0
votes
2 answers

Misuse of guard statement to replace nil checking

I'm doing something really simple just to get used to Swift (coming from objc) - I want to return a desired node in a linked list by using a guard statement and a switch statement. I'm obviously misusing the guard statement because my else clause is…
0
votes
1 answer

How to guard against invalid instantiateViewControllerWithIdentifier in Swift

In Swift, how do I guard against calling storyboard.instantiateViewControllerWithIdentifier on an identifier that's not valid? Most solutions I have seen on SO uses try...catch in Objective-C, but the solution does not work in Swift because…
Boon
  • 40,656
  • 60
  • 209
  • 315
0
votes
3 answers

Error with a quadrant giving function

I tried to make a function to give the quadrant name when inputting the x and y coordinates. However, I am getting the error: "parse error on input ‘=’ Failed, modules loaded: none." I tried adding a "|otherwise.." but that still didn't work. I…
JJ chips
  • 97
  • 5
0
votes
1 answer

Swift 2.0 Guard Statement Fails Struct Initializer

There was a similarly named topic but the example was an error due to user mistake. I believe this example is an actual XCode issue. I was following a treehouse tutorial and in the spirit of swift 2.0 I used guard statements instead of if lets in…
R.P. Carson
  • 439
  • 5
  • 20
0
votes
3 answers

My first guard, is it appropriate here?

I'm playing with Swift 2 and I'm looking through my code for instances where I'm guarding and I might want to use guard. Here's one... var mods : String = "" let modpath = NSBundle.mainBundle().pathForResource(filename, ofType: "ini", inDirectory:…
Maury Markowitz
  • 9,082
  • 11
  • 46
  • 98
-3
votes
1 answer

Why do I need "!" to use an array parameter while using a guard statement?

Currently focused on structures and algorithms and I came across this one. import Foundation let numbers = [1, 3, 56, 66, 68, 80, 99, 105, 450] func naiveContains(_ value: Int, in array: [Int]) -> Bool { guard !array.isEmpty else { return…
JLico
  • 27
  • 1
  • 7
-4
votes
3 answers

Guard operation Swift 2

I use Swift 2 and Xcode 7. I would like to know the difference between if condition { ... } else { ... } and guard ... else ...
hgcahez
  • 413
  • 6
  • 20
1 2
3