Is there a statement in Swift that stops evaluating a conditional expression with multiple expressions as soon as the answer is clear?

In some programming language, there are two more operators in addition to the simple || and & &. these operators, which I will call them _orif and _andif, can now be used instead of && and || and they can help improve efficiency and avoid mistakes, since evaluating conditional stops as soon as the answer is clear.

For example, evaluating the following expression will stop halfway through (selectedSprite != nil): false. Thus, the rest of the conditional value will be ignored and never evaluated, this will prevent a fatal error in this case: fatal error: unexpectedly found nil while unwrapping an Optional valueand it will go up to the second expression, because obviously nil is not responding SpriteOwner().

if (selectedSprite != nil) &&
   (selectedSprite.SpriteOwner().type == "Human")
{
   println("a human selected")
}

I'm looking for a replacement && & in the above code snippet that can be used instead of a simple && operator, so if the first expression evaluates to false (with selectedSpritenil equal), then the second expression is ignored altogether (since it does not affect the result)

Question:

Is there such a statement &&?in swift? if the answer is no,

Is there a better way to do this instead of nested if statements, as I wrote here:

if (selectedSprite != nil)
{
    if (selectedSprite.SpriteOwner().type == "Human")
    {
       println("a human selected")
    }
}

if, , if nil .

+4
3

, , , Swift . :

let a : Int? = nil

if a != nil && a! == 1 {
    print("a is 1")
} else {
    print("a is nil")
}

, a . , , SpriteOwner() nil. Swifty - let ... where ...:

if let s = selectedSprite where s.SpriteOwner().type == "Human" {
    println("a human selected")
}
+4

|| && Swift 2 , . . " " " ": https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/BasicOperators.html#//apple_ref/doc/uid/TP40014097-CH6-ID60

false, . , , , true. .

; , , :

func isBar(rv: Bool) -> Bool {
    print("side effect of isBar")
    return rv
}

func isFoo() -> Bool {
    print("side effect of isFoo")
    return true
}

if isBar(false) && isFoo() {
    print("both true")
}
+4

You can achieve something similar with the optional chain:

class Sprite {
    let otherSprite: Sprite?
    let type: String
    init(otherSprite: Sprite?, type: String) {
        self.otherSprite = otherSprite
        self.type = type
    }
}

let innerSprite = Sprite(otherSprite: nil, type: "Human")
let outerSprite = Sprite(otherSprite: innerSprite, type: "Robot")

This does not execute the print statement because they are otherSprite? returns with zero

if innerSprite.otherSprite?.type == "Human" {
    print("A human selected")
}

This will print “Man selected” because the value of otherSprite is not nil and the value is of type == Human>

if outerSprite.otherSprite?.type == "Human" {
    print("A human selected")
}
0
source

Source: https://habr.com/ru/post/1608581/


All Articles