You can use optional binding: if letto check if there is something nil.
Example 1:
if let text = history.text where !text.isEmpty {
history.text! += "\ncontent"
} else {
history.text = digit
}
Or you can use mapto check additional parameters:
Example 2:
history.text = history.text.map { !$0.isEmpty ? $0 + "\ncontent" : digit } ?? digit
!$0.isEmpty in most cases itβs not even needed, so the code may look a little better:
history.text = history.text.map { $0 + "\ncontent" } ?? digit
EDIT: What does map:
.
, Ints, , , , "β¬", .. [10,20,45,32] -> ["10β¬","20β¬","45β¬","32β¬"].
- , ,
var stringsArray = [String]()
for money in moneyArray {
stringsArray += "\(money)β¬"
}
map :
let stringsArray = moneyArray.map { "\($0)β¬" }
:
, - -. , , i, . i.map {$ 0 * 2}. , . , , .
()
??:
nil coalescing (a? b) a, , b, a nil. a . b , .
nil :
a != nil ? a! : b