测试数组是否包含至少一个具有某些属性的对象

时间:2018-05-15 08:36:07

标签: ios swift testing

我想测试给定数组是否包含至少一个包含特定“字符串”的对象。它有用吗?

3 个答案:

答案 0 :(得分:1)

尝试filter()

struct S { let string: String }

let array = [ S(string: "a"), S(string: "b") ]

let hasAtleastOneA = array.filter({ $0.string == "a" }).count > 0

答案 1 :(得分:0)

类似的东西:

let array = ["a","b","c"]

if array.count > 0 {
    for name in array {
        if name.contains("a"){
            print("YES")
        }

    }
}

答案 2 :(得分:0)

你检查这样做,

CurrentProject.Connection.Execute

Reference Higher order functions in swift: Filter, Map, Reduce, flatmap