上QQ阅读APP看书,第一时间看更新
filter
The way filter works is quite simple. It iterates through a collection, applying a test to each element. Each element that passes the test is added to a new collection, which is returned:
let primes = [2,3,5,7,11,13,17,19,23]
let singleDigitPrimes = primes.filter({$0 <10})
The preceding code returns an array of single digit primes, leaving the primes array unchanged.
let intStrings = [1:"1",2:"4", 3:"3"]
let results = intStrings.filter { key, str in"\(key)"== str }
The preceding code returns a dictionary that does not contain the entry 2: "4", since it failed the test in the closure.