swift - Exclamation mark prefix on variables during if statements and other as well? -
i'm confused after looking through similar questions of the(!) operator when prefixed on variable or other object in if statements, functions, etc?
example: mutating func add(value: t) { if !contains(items, value) { items.append(value) } }
the exclamation mark !
used 2 purposes. when see appear @ beginning of object, such in !contains(items, values)
, means "not". example...
let x = 10 let y = 5 if x == y { print("x equal y") } else if x != y { print("x not equal y") }
the above code print => "x not equal y
" .
the logical not (!
) operator can used reverse boolean values. example...
var falseboolvalue = false falseboolvalue = !falseboolvalue print(falseboolvalue)
the above code print => "true
"
in addition usage logical not operator, exclamation mark used implicitly unwrap optional values. whenever see exclamation mark appear @ end of object name, such in somevariable!
, being used implicitly unwrap optional value. read optionals gain better understanding of how !
used optional values.
Comments
Post a Comment