Swift map和filter函數(shù)原型基礎(chǔ)示例
map函數(shù)原型
/// Returns an array containing the results of mapping the given closure /// over the sequence's elements. /// /// In this example, `map` is used first to convert the names in the array /// to lowercase strings and then to count their characters. /// /// let cast = ["Vivien", "Marlon", "Kim", "Karl"] /// let lowercaseNames = cast.map { $0.lowercased() } /// // 'lowercaseNames' == ["vivien", "marlon", "kim", "karl"] /// let letterCounts = cast.map { $0.count } /// // 'letterCounts' == [6, 6, 3, 4] /// /// - Parameter transform: A mapping closure. `transform` accepts an /// element of this sequence as its parameter and returns a transformed /// value of the same or of a different type. /// - Returns: An array containing the transformed elements of this /// sequence. @inlinable public func map<T>(_ transform: (Element) throws -> T) rethrows -> [T]
let cast = ["Vivien", "Marlon", "Kim", "Karl"] let lowercaseNames = cast.map { $0.lowercased() } print(lowercaseNames) // ["vivien", "marlon", "kim", "karl"] let arrayString = ["Ann", "Bob", "Tom", "Lily", "HanMeiMei", "Jerry"] // 計(jì)算每個(gè)元素的個(gè)數(shù),生成個(gè)數(shù)數(shù)組 let arrayCount = arrayString.map { (str) -> Int in return str.count } print("arrayCount: \(arrayCount)") // arrayCount: [3, 3, 3, 4, 9, 5]
filter函數(shù)原型
// @inlinable public func filter(_ isIncluded: (Element) throws -> Bool) rethrows -> [Element] let array = [-5, 4, -3, 1, 2] var resultArray = array.filter { (item) -> Bool in return item > 0 } print(resultArray) // [4, 1, 2] // 語法糖寫法 resultArray = array.filter { $0 > 0 } print(resultArray) // [4, 1, 2]
以上就是Swift map和filter函數(shù)原型基礎(chǔ)示例的詳細(xì)內(nèi)容,更多關(guān)于Swift map filter函數(shù)原型的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
swift 3.0中實(shí)現(xiàn)字符串截取、比較的方法示例
時(shí),為了使用現(xiàn)有的字符串生成一個(gè)新的字符串,我們可以使用截取字符串的方法實(shí)現(xiàn)。下面這篇文章主要給大家介紹了關(guān)于swift 3.0中實(shí)現(xiàn)字符串截取的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒。2017-10-10Swift調(diào)用Objective-C編寫的API實(shí)例
這篇文章主要介紹了Swift調(diào)用Objective-C編寫的API實(shí)例,介紹的比較全面和詳細(xì),對(duì)Objective-C代碼的重復(fù)利用有極大好處,的朋友可以參考下2014-07-07iPhone與iWatch連接、控制、數(shù)據(jù)傳遞(Swift)的方法
這篇文章主要介紹了iPhone與iWatch連接、控制、數(shù)據(jù)傳遞(Swift)的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03Swift如何為網(wǎng)頁(yè)承載頁(yè)面添加更多功能詳解
這篇文章主要給大家介紹了關(guān)于Swift如何為網(wǎng)頁(yè)承載頁(yè)面添加更多功能的相關(guān)資料,包括添加菊花加載的效果、添加跳轉(zhuǎn)到Safari的功能、添加復(fù)制鏈接的功能以及添加分享網(wǎng)頁(yè)的功能,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-05-05SwiftUI開發(fā)總結(jié)combine原理簡(jiǎn)單示例詳解
這篇文章主要為大家介紹了SwiftUI開發(fā)總結(jié)combine原理簡(jiǎn)單示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02深入探究Swift枚舉關(guān)聯(lián)值的內(nèi)存
這篇文章主要給大家介紹了關(guān)于Swift枚舉關(guān)聯(lián)值的內(nèi)存的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Swift具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Swift 中如何使用 Option Pattern 改善可選項(xiàng)的 API 設(shè)計(jì)
這篇文章主要介紹了Swift 中如何使用 Option Pattern 改善可選項(xiàng)的 API 設(shè)計(jì),幫助大家更好的進(jìn)行ios開發(fā),感興趣的朋友可以了解下2020-10-10