Skip to content

Instantly share code, notes, and snippets.

@nanoxd
Last active September 22, 2018 16:45
Show Gist options
  • Save nanoxd/9603e1c69aa35c3506c58e9420d8c339 to your computer and use it in GitHub Desktop.
Save nanoxd/9603e1c69aa35c3506c58e9420d8c339 to your computer and use it in GitHub Desktop.
[UIEdgeInsets+DictionaryLiteral] Use a dictionary literal for UIEdgeInsets #swift #uikit
extension UIEdgeInsets: ExpressibleByDictionaryLiteral {
public typealias Key = EdgeKey
public typealias Value = CGFloat
public enum EdgeKey {
case top
case left
case bottom
case right
}
func keyPath(forEdge edgeKey: EdgeKey) -> WritableKeyPath<UIEdgeInsets, CGFloat> {
switch edgeKey {
case .top: return \UIEdgeInsets.top
case .left: return \UIEdgeInsets.left
case .bottom: return \UIEdgeInsets.bottom
case .right: return \UIEdgeInsets.right
}
}
public init(dictionaryLiteral elements: (EdgeKey, CGFloat)...) {
self = UIEdgeInsets()
for (edge, value) in elements {
let keyPath = self.keyPath(forEdge: edge)
self[keyPath: keyPath] = value
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment