How to check if an element is in an array

后端 未结 17 1131
囚心锁ツ
囚心锁ツ 2020-11-22 10:24

In Swift, how can I check if an element exists in an array? Xcode does not have any suggestions for contain, include, or has, and a qu

17条回答
  •  爱一瞬间的悲伤
    2020-11-22 10:50

    Swift 4/5

    Another way to achieve this is with the filter function

    var elements = [1,2,3,4,5]
    if let object = elements.filter({ $0 == 5 }).first {
        print("found")
    } else {
        print("not found")
    }
    

提交回复
热议问题