Swift filter array of strings

后端 未结 4 2063
日久生厌
日久生厌 2021-02-20 15:49

I\'ve had troubles filtering array of keywords (strings) in swift ,My code:

self.filteredKeywords=filter(keywords.allValues, {(keyword:NSString) ->                    


        
4条回答
  •  臣服心动
    2021-02-20 16:07

    [Updated for Swift 2.0]

    As NSString is toll-free bridged to Swift String, just avoid the coercions with:

      3> ["abc", "bcd", "xyz"].filter() { nil != $0.rangeOfString("bc") }
    $R1: [String] = 2 values {
      [0] = "abc"
      [1] = "bcd"
    }
    

    But, if you think allValues aren't strings:

    (keywords.allValues as? [String]).filter() { nil != $0.rangeOfString("bc") }
    

    which returns an optional array.

提交回复
热议问题