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", ""].concatenate()
Swift Array Extensions


Tagged as:


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

You are commenting using your 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.