Last active
October 6, 2018 16:20
-
-
Save nanoxd/b2ef0818c736b957933e4a35859c8a2a to your computer and use it in GitHub Desktop.
[Custom Namespace] An example of how to create a custom namespace for your app to avoid clobbering existing names #swift
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
import Foundation | |
/** | |
A custom namespace _へ__(‾◡◝ )>. | |
General pattern would be: | |
``` | |
extension CustomNamespace where Base: UIView { | |
// This property does not override the existing one | |
var safeAreaInsets: UIEdgeInsets { | |
return .zero | |
} | |
} | |
view.cu.safeAreaInsets | |
``` | |
With this approach we can have more specialized methods and properties using | |
`Base` and not just specialized on a common base type. | |
*/ | |
final class CustomNamespace<Base> { | |
let base: Base | |
init(_ base: Base) { | |
self.base = base | |
} | |
} | |
/// A protocol that boxes up the type into CustomNamespace automatically | |
protocol CustomNamespaceCompatible { } | |
extension CustomNamespaceCompatible { | |
static var cu: CustomNamespace<Self>.Type { | |
return CustomNamespace<Self>.self | |
} | |
var cu: CustomNamespace<Self> { | |
return CustomNamespace(self) | |
} | |
} | |
// MARK: - Default Extensions | |
extension NSObject: CustomNamespaceCompatible { } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment