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
1
vote
1 answer

Swift Pattern Matching - multiple enum-case-pattern in guard?

What's the correct syntax for using enum cases and guard to allow more-than-one case to proceed? With a switch we can use a case-item-list to combine switch cases. Is there anything like that for guard or if statements? Here's a code example of…
edelaney05
  • 6,822
  • 6
  • 41
  • 65
1
vote
1 answer

Are "guard let" and "if let" different statements than "guard" and "if"?

I know a guard statement can be used like this guard let someConstant = someOptional() else { // ... } And I tried to do struct MyStruct { let aString: String init?() { guard aString = optionalString() else { return } } //…
kumowoon1025
  • 201
  • 1
  • 9
1
vote
2 answers

Haskell IO indentation

I tried to rewrite that program, which is working: nameIOite :: IO () nameIOite = do putStrLn "What's your name ?" name <- getLine if name `elem` ["Simon","John","Phil"] --if name == "Simon" || name == "John" || name == "Phil" also…
Kitsune
  • 117
  • 5
1
vote
3 answers

How to elegantly combine a guard statement with a condition?

I currently have the guard statement: guard let designationQuota = Defaults.quotas.value?.designationQuota, designationQuota > 0 else { return AppDelegate.shared.presentNoDesignationQuotaWarning() } however I only want to do the guard block…
Kex
  • 8,023
  • 9
  • 56
  • 129
1
vote
2 answers

How does swift guard determine true or false when using = operator

Learning swift 3.1 by reading Language Guide (developer.apple.com). I learned that in swift the assignment operator (=) does not return a value. In control flow chapter got an example of guard statement: func greet(person: [String: String]) { …
MD TAREQ HASSAN
  • 1,188
  • 20
  • 46
1
vote
2 answers

Conditionally binding to an existing property

I'm looking for a way to improve this pattern of code: struct Struct { let i: Int init?(i: Int?) { guard let unwrappedI = i else { return nil } self.i = unwrappedI } } It'd be nice to remove the unwrappedI temporary…
Alexander
  • 59,041
  • 12
  • 98
  • 151
1
vote
2 answers

How to Get Test Coverage for Guard Statement Fall-through

I started writing iOS unit tests today with the BDD approach. I have a question regarding guard statements and getting to 100% code coverage. I have the following code, which handles the conversion of Data into Customer objects. internal final class…
Nick Kohrn
  • 5,779
  • 3
  • 29
  • 49
1
vote
4 answers

How to use guard in swift instead of if

How to use 'guard' in swift.I have gone through many articles about 'guard'.But i didnt get clear idea about this.Please give me clear idea.Please give me sample output for following 'if' statement. if firstName != "" { if lastName != "" { …
Madhumitha
  • 3,794
  • 8
  • 30
  • 45
1
vote
1 answer

Swift guard statement usage

Depending on my understanding of guard statement in swift I am doing the following: func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let identifier = "identifier" let dequeCell =…
Vivek Molkar
  • 3,910
  • 1
  • 34
  • 46
0
votes
1 answer

is this firestore closure causing a memory leak?

So my goal is to fix this condition issue when it comes to instantiating the right viewController. I have a function that I basically use to navigate a user to the right viewController depending on the type of user and if they're logged in or…
0
votes
0 answers

How to unit test a computed property in Swift?

How can I unit test the guard statement of a computed property in swift? When the unit tests are run, the fataError line is not included in the code coverage. Is there any way to test the computed properties? class HomePresenter:…
0
votes
1 answer

Haskell Compilation Error - Guard Type mismatching

I've been stuck at this particular stage working on a problem I've been given, and my experience with haskell is still at a beginner level. In trying to create a function to create a insert a Node (consisting of a String "Key" and a Value) into a…
Eoin Dowling
  • 373
  • 3
  • 15
0
votes
0 answers

How to use UISegmentedControl in guard together with textfields

I am currently facing a problem where I want to use guards to enable a button if desired information are filled out. After reading into the guard mechanism one thing is unclear for me. How do i combine UITextFields with UISegmentedControls so that…
user12193751
0
votes
2 answers

Execute multiple statements based on boolean conditions in Haskell

I am new to Haskell I am trying to execute multiple statements if the several boolean values are true. I tried it using guards but it only executes the first statement that is true and it stops. I want it to execute all of them that are true, for…
user9156598
0
votes
1 answer

expected expression in conditional

I have written the following function and am getting the following error in the guard statement. expected expression in conditional func containsNearbyDuplicate(_ nums: [Int], _ k: Int) -> Bool { // form a dictionary key is the number, value…
casillas
  • 16,351
  • 19
  • 115
  • 215