Note: Advanced Swift Tricks: Custom Array Extensions

Author: Lets Build That App
Video URL: Advanced Swift Tricks: Custom Array Extensions

import UIKit

extension Array where Element: Numeric {
    func customSum() -> Element {
        //return self.reduce(0, {$0 + $1})
        return self.reduce(0, +)
    }
}

[2.3, 3, 4.7].customSum()

extension Array where Element == String {
    func concatenate() -> String {
        return self.reduce("", {$0 + $1 + " "})
    }
}

["Hello", "From", "wangmingjun.com"].concatenate()
Swift Array Extensions
Advertisements

分类:iOS

Tagged as:

发表评论

Fill in your details below or click an icon to log in:

WordPress.com 徽标

You are commenting using your WordPress.com account. Log Out /  更改 )

Google photo

You are commenting using your Google account. Log Out /  更改 )

Twitter picture

You are commenting using your Twitter account. Log Out /  更改 )

Facebook photo

You are commenting using your Facebook account. Log Out /  更改 )

Connecting to %s

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理