[英]How should I use a guard statement correctly with RxSwift Singles?
[英]Why do I need "!" to use an array parameter while using a guard statement?
目前專注於結構和算法,我遇到了這個。
import Foundation
let numbers = [1, 3, 56, 66, 68, 80, 99, 105, 450]
func naiveContains(_ value: Int, in array: [Int]) -> Bool {
guard !array.isEmpty else { return false }
let midleIndex = array.count / 2
if value <= array[midleIndex] {
for index in 0...midleIndex {
if array[index] == value {
return true
}
}
} else {
for index in midleIndex..<array.count {
if array[index] == value {
return true
}
}
}
return false
}
我的問題的確切位置是警衛聲明:
guard !array.isEmpty else { return false }
我不確定為什么 guard 聲明需要 . 在 !array.isEmpty
我只需要幫助理解為什么感嘆號需要放在數組參數之前。
謝謝!
該算法要求數組不為空。 . 意思不是。 So.array.isEmpty 表示不為空。
guard...
的含義就像if not...
所以guard.array.isEmpty
意味着if not not array is empty
,這又等同於if array.isEmpty {return false}
我可以看到它很混亂!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.