Swift 中的过滤器

tgot5729 8年前
   <p>Swift 提供了很多方便的函数来操作数组,比如 <code>filter</code> 和 <code>map</code>。在这篇文章里,我将带大家熟悉一下 filter,以及学习如何使用 map 来组合出新的数组。</p>    <p>假设你有一个数组,接着你想要创建一个新的数组,这个数组包含原数组中所有大于 10 的元素,你可以使用下面的 for 循环:</p>    <table>     <tbody>      <tr>       <td> <pre>  <code class="language-swift">let anArray = Array(1...20)     var anotherArray = [Int]()     for i in anArray {      if i > 10 {          anotherArray.append(i)      }  }     print(anotherArray) // [11,12,13,14,15,16,17,18,19,20]  </code></pre> </td>      </tr>     </tbody>    </table>    <p>除了代码多点,也没什么问题。</p>    <h2>Filter</h2>    <p>但是你可以尝试使用一下 <code>filter</code> 函数,这是 Swift 为每个数组提供的一个新式武器,可以大大缩减枚举的代码量:</p>    <table>     <tbody>      <tr>       <td> <pre>  <code class="language-swift">let anotherArray = anArray.filter({ (a:Int) -> Bool in      return a > 10  })     print(anotherArray)  </code></pre> </td>      </tr>     </tbody>    </table>    <p>该函数带一个闭包做为参数,这个闭包将数组中的元素作为参数,并返回一个 <code>bool</code> 结果。数组中的每一个元素都会执行该闭包,根据返回的结果来决定是否应存在于新的数组中。</p>    <p>通过 Swift 提供的闭包简化写法,我们可以进一步精简:</p>    <table>     <tbody>      <tr>       <td> <pre>  <code class="language-swift">let anotherArray = anArray.filter ({$0 > 10})     print(anotherArray) // [11,12,13,14,15,16,17,18,19,20]  </code></pre> </td>      </tr>     </tbody>    </table>    <p>和最初的版本对比一下,是不是精简了许多呢:)。</p>    <h2>使用 map 来组合</h2>    <p>数组还提供了一个有趣的函数 <code>map</code>,该函数同样是带一个闭包作为参数并且在内部返回一个经过转换的元素。所以我们先筛选出数组中所有大于 10 的元素,然后让它们的值翻一倍:</p>    <table>     <tbody>      <tr>       <td> <pre>  <code class="language-swift">let anArray = Array(1...20)     let anotherArray = anArray.filter({$0 > 10}).map({$0 * 2})     print(anotherArray) // [22, 24, 26, 28, 30, 32, 34, 36, 38, 40]  </code></pre> </td>      </tr>     </tbody>    </table>    <p>关于 map 更多的细节,可以查看我<a href="http://www.open-open.com/lib/view/open1462934539136.html">此前写的一篇文章</a></p>    <p>当然你只能在条件不太复杂时这么做,如果情况比较复杂,这种写法将使代码变得更加难读。大体来说就是,如果为了可读性,那么多写点代码还是值得的。</p>    <p>来自:http://swift.gg/2016/05/10/swift-filter/</p>