Skip to content

Instantly share code, notes, and snippets.

@vialyx
Last active April 20, 2018 11:09
Show Gist options
  • Save vialyx/8401b444b76c5c465d42770cf39e861c to your computer and use it in GitHub Desktop.
Save vialyx/8401b444b76c5c465d42770cf39e861c to your computer and use it in GitHub Desktop.
//: 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