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

This site uses Akismet to reduce spam. Learn how your comment data is processed.