Do nothing if inside quickly

I am wondering inside the statement if, can I have something like Python pass?

var num = 10
num > 5 ? doSomethingIfTrue(): doSomethingIfFalse()

The code above will be fine if both true and false methods are provided. What if I only want to execute only the true method? For instance:

num > 5 ? doSomethingIfTrue(): ***pass***

I hope to get something like instructions passin swift, so the program will continue to work if false is returned. I tried continueand fallthrough, but I assume that they are used only in the loop statement.

+4
source share
4 answers

You could theoretically say the following:

var num = 10
num > 5 ? doSomethingIfTrue() : ()

This works because it ()is a void operator.

! :

var num = 10
if num > 5 { doSomethingIfTrue() }

, , Swift; if. . - , .

, , . Swift Python; .

+5

?: , . ,

a = b + ((c > 0) ? 1 : pass)

+, c ?

if :

if num > 5 {
  doSomethingIfTrue()
}
+1

, , , , - - if.

, , void doSomethingIfTrue() ().

, , . doSomethingIfTrue() void, Int, . 0 - .

( ):

let pass: Any = ()

func doSomething() { }
func doSomethingElse() -> String  { return "a" }
func doSomethingNumeric() -> Int  { return 1 }

var num = 5
num > 5 ? doSomething() : pass
num > 5 ? doSomethingElse() : pass
num > 5 ? doSomethingNumeric() : pass

- , , Any.

: -)

+1

else ? . :

var num = 10

if num > 5 {
    println("hi")
}

var num = 10

num > 5 ? doSomethingIfTrue(): pass()

pass() , , .

0

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


All Articles