Skip to content

Instantly share code, notes, and snippets.

@nanoxd
Last active October 6, 2018 16:20
Show Gist options
  • Save nanoxd/b2ef0818c736b957933e4a35859c8a2a to your computer and use it in GitHub Desktop.
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
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