Skip to content

Instantly share code, notes, and snippets.

@volonbolon
Created December 18, 2017 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save volonbolon/0a04eab78fbc06d73ad6fac9e9748fc8 to your computer and use it in GitHub Desktop.
Save volonbolon/0a04eab78fbc06d73ad6fac9e9748fc8 to your computer and use it in GitHub Desktop.
Example of ``
//: Playground - noun: a place where people can play
import UIKit
import CoreImage
enum Color: Int {
case green = 1001
case blue
case black
case yellow
}
extension Color: CustomStringConvertible {
var description: String {
var description: String
switch self {
case .green:
description = "Green"
case .blue:
description = "Blue"
case .black:
description = "Black"
case .yellow:
description = "Yellow"
}
return description
}
}
let greenView = UIView()
greenView.tag = Color.green.rawValue
let blueView = UIView()
blueView.tag = Color.blue.rawValue
let blackView = UIView()
blackView.tag = Color.black.rawValue
let yellowView = UIView()
yellowView.tag = Color.yellow.rawValue
yellowView.addSubview(blackView)
blackView.addSubview(blueView)
blueView.addSubview(greenView)
let seq = sequence(state: greenView) { (next: inout UIView) -> UIView? in
let parent = next.superview
next = parent != nil ? parent! : next
return parent // The top most `parent` will be nil, stopping the execution of the closure
}
let greenColor = Color(rawValue: greenView.tag)!
var hierarchy = "\(greenColor)"
for v in seq {
if let color = Color(rawValue: v.tag) {
hierarchy += "->\(color)"
}
}
print("hierarchy: \(hierarchy)") // hierarchy: Green->Blue->Black->Yellow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment