美文网首页
Swift filter 函数

Swift filter 函数

作者: 大刘 | 来源:发表于2023-01-04 15:48 被阅读0次
image.png
// @inlinable public func filter(_ isIncluded: (Element) throws -> Bool) rethrows -> [Element]

let array = [-5, 4, -3, 1, 2]
var resultArray = array.filter { (item) -> Bool in
    return item > 0
}
print(resultArray) // [4, 1, 2]

// 语法糖写法
resultArray = array.filter {
    $0 > 0
}
print(resultArray) // [4, 1, 2]

相关文章

网友评论

      本文标题:Swift filter 函数

      本文链接:https://www.haomeiwen.com/subject/ryslcdtx.html