Swift:map 和 flatMap 基础入门

daqian 8年前
   <p>借助于 map和flapMap 函数能够很轻易地将数组转换成另外一个新数组。</p>    <p>Map</p>    <p><code>map</code>函数能够被数组调用,它接受一个闭包作为参数,作用于数组中的每个元素。闭包返回一个变换后的元素,接着将所有这些变换后的元素组成一个新的数组。</p>    <p>这听起来有些复杂,但它是相当简单的。想象你拥有一个string类型的数组:</p>    <table>     <tbody>      <tr>       <td> <pre>  <code class="language-swift">let testArray = ["test1","test1234","","test56"]  </code></pre> </td>      </tr>     </tbody>    </table>    <p><code>map</code>函数的闭包接收一个字符串(类型为<code>string</code>)作为参数,原因在于我们调用函数处理的数组元素类型为<code>String</code>。本例中,我们想要返回一个整型数组,逐个对应字符串元素成员的字符长度。因此闭包的返回类型为<code>Int?</code>.</p>    <table>     <tbody>      <tr>       <td> <pre>  <code class="language-swift">let anotherArray = testArray.map { (string:String) -> Int? in              let length = string.characters.count              guard length > 0 else {           return nil       }          return string.characters.count  }     print(anotherArray) //[Optional(5), Optional(8), nil, Optional(6)]  </code></pre> </td>      </tr>     </tbody>    </table>    <p>FlatMap</p>    <p><code>flatMap</code>很像<code>map</code>函数,但是它摒弃了那些值为<code>nil</code>的元素。</p>    <table>     <tbody>      <tr>       <td> <pre>  <code class="language-swift">let anotherArray2 = testArray.flatMap { (string:String) -> Int? in          let length = string.characters.count          guard length > 0 else {            return nil       }          return string.characters.count  }     print(anotherArray2) //[5, 8, 6]  </code></pre> </td>      </tr>     </tbody>    </table>    <p>另外一个与<code>map</code>函数不同之处在于:倘若元素值不为nil情况下,<code>flapMap</code>函数能够将可选类型(<code>optional</code>)转换为非可选类型(<code>non-optionals</code>)。</p>    <p> </p>    <p>来自:http://swift.gg/2015/11/26/swift-map-and-flatmap/</p>