1
是否有任何这样的语句可以做类似于下面的操作?还是我需要创建一个函数?是否有类似于Swift 3.0中的“If Any”的语句?Xcode
let x=[Double](1.023, 2.023, 3.023, 4.023, 5.023)
ler y=[Double](3.001)
if any of x > y{
("YES")}
是否有任何这样的语句可以做类似于下面的操作?还是我需要创建一个函数?是否有类似于Swift 3.0中的“If Any”的语句?Xcode
let x=[Double](1.023, 2.023, 3.023, 4.023, 5.023)
ler y=[Double](3.001)
if any of x > y{
("YES")}
您可以使用Array的contains(where:)
方法。
let x = [1.023, 2.023, 3.023, 4.023, 5.023]
let y = 3.001
if x.contains(where: { $0 > y }) {
print("YES")
}
如果你想知道这是更大的第一个值,你可以这样做:
let x = [1.023, 2.023, 3.023, 4.023, 5.023]
let y = 3.001
if let firstLarger = x.first(where: { $0 > y }) {
print("Found \(firstLarger)")
}
如果你想知道的一切都较大,您可以使用filter
。
let x = [1.023, 2.023, 3.023, 4.023, 5.023]
let y = 3.001
let matches = x.filter { $0 > y }
print("The following are greater: \(matches)")
let x = [1.023, 2.023, 3.023, 4.023, 5.023]
let y = [3.001]
let result = x.filter { $0 > y[0] }
print(result) // [3.023, 4.023, 5.023]
这不是斯威夫特。发布您的实际代码尝试。如果y只是Double(不是数组),那么可以使用contains(where :):如果x.contains(其中:{$ 0> y}){ print(true) } –