How to search an Array containing struct elements in Swift?

后端 未结 1 682
长发绾君心
长发绾君心 2020-12-30 00:23

It\'s kind pretty straight forward to find an element in an array with type String, Int, etc.

var States = [\"CA\", \"FL\", \"MI\"]
var filteredStates = Stat         


        
相关标签:
1条回答
  • 2020-12-30 00:45

    With the following code you receive all candy structs in the array, which match to "Chocolate".

    var candiesFiltered = candies.filter{$0.name == "Chocolate"}
    

    If you just want a boolean if it has been found or not you could use the following code:

    var found = candies.filter{$0.name == "Chocolate"}.count > 0
    
    0 讨论(0)
提交回复
热议问题