Last active
April 20, 2018 11:09
-
-
Save vialyx/8401b444b76c5c465d42770cf39e861c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
import UIKit | |
enum PersonalAccount { | |
case credit(amount: Int64, id: UInt) | |
case debit(amount: UInt64, id: UInt) | |
} | |
let accounts: [PersonalAccount] = [.credit(amount: -1000, id: 1), | |
.debit(amount: 1000, id:2), | |
.credit(amount: -1000000, id: 3)] | |
// MARK: Regular construction | |
for account in accounts { | |
if case let .credit(amount, id) = account { | |
print("amount: \(amount) id: \(id)") | |
} | |
} | |
// MARK: Using pattern matching | |
for case let .credit(amount, id) in accounts { | |
print("amount: \(amount) id: \(id)") | |
} | |
/* | |
amount: -1000 id: 1 | |
amount: -1000000 id: 3 | |
*/ | |
// MARK: One more example using 'where' keyword | |
let views: [UIView] = [UIButton(), UILabel(), UIScrollView(), UILabel()] | |
for view in views where view is UILabel { | |
print("view class: \(view.self)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment