Last active
September 22, 2018 16:45
-
-
Save nanoxd/9603e1c69aa35c3506c58e9420d8c339 to your computer and use it in GitHub Desktop.
[UIEdgeInsets+DictionaryLiteral] Use a dictionary literal for UIEdgeInsets #swift #uikit
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
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