Commit 6ab2352e by Daniel Dahan

development: updated AssociatedObject API to a struct from global functions

parent fe8a6c41
......@@ -141,12 +141,12 @@ extension UIView {
/// TabBarItem reference.
public private(set) var divider: Divider {
get {
return AssociatedObject(base: self, key: &DividerKey) {
return AssociatedObject.get(base: self, key: &DividerKey) {
return Divider(view: self)
}
}
set(value) {
AssociateObject(base: self, key: &DividerKey, value: value)
AssociatedObject.set(base: self, key: &DividerKey, value: value)
}
}
......
......@@ -272,12 +272,12 @@ extension UIView {
/// Grid reference.
public var grid: Grid {
get {
return AssociatedObject(base: self, key: &GridKey) {
return AssociatedObject.get(base: self, key: &GridKey) {
return Grid(context: self)
}
}
set(value) {
AssociateObject(base: self, key: &GridKey, value: value)
AssociatedObject.set(base: self, key: &GridKey, value: value)
}
}
......
......@@ -968,12 +968,12 @@ extension UIView {
/// Layout reference.
public private(set) var layout: Layout {
get {
return AssociatedObject(base: self, key: &LayoutKey) {
return AssociatedObject.get(base: self, key: &LayoutKey) {
return Layout(parent: self)
}
}
set(value) {
AssociateObject(base: self, key: &LayoutKey, value: value)
AssociatedObject.set(base: self, key: &LayoutKey, value: value)
}
}
......
......@@ -100,12 +100,12 @@ extension CALayer {
/// MaterialLayer Reference.
fileprivate var materialLayer: MaterialLayer {
get {
return AssociatedObject(base: self, key: &MaterialLayerKey) {
return AssociatedObject.get(base: self, key: &MaterialLayerKey) {
return MaterialLayer(layer: self)
}
}
set(value) {
AssociateObject(base: self, key: &MaterialLayerKey, value: value)
AssociatedObject.set(base: self, key: &MaterialLayerKey, value: value)
}
}
......
......@@ -28,30 +28,32 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
Gets the Obj-C reference for the instance object within the UIView extension.
- Parameter base: Base object.
- Parameter key: Memory key pointer.
- Parameter initializer: Object initializer.
- Returns: The associated reference for the initializer object.
*/
internal func AssociatedObject<T: Any>(base: Any, key: UnsafePointer<UInt8>, initializer: () -> T) -> T {
if let v = objc_getAssociatedObject(base, key) as? T {
internal struct AssociatedObject {
/**
Gets the Obj-C reference for the instance object within the UIView extension.
- Parameter base: Base object.
- Parameter key: Memory key pointer.
- Parameter initializer: Object initializer.
- Returns: The associated reference for the initializer object.
*/
public static func get<T: Any>(base: Any, key: UnsafePointer<UInt8>, initializer: () -> T) -> T {
if let v = objc_getAssociatedObject(base, key) as? T {
return v
}
let v = initializer()
objc_setAssociatedObject(base, key, v, .OBJC_ASSOCIATION_RETAIN)
return v
}
let v = initializer()
objc_setAssociatedObject(base, key, v, .OBJC_ASSOCIATION_RETAIN)
return v
}
/**
Sets the Obj-C reference for the instance object within the UIView extension.
- Parameter base: Base object.
- Parameter key: Memory key pointer.
- Parameter value: The object instance to set for the associated object.
- Returns: The associated reference for the initializer object.
*/
internal func AssociateObject<T: Any>(base: Any, key: UnsafePointer<UInt8>, value: T) {
objc_setAssociatedObject(base, key, value, .OBJC_ASSOCIATION_RETAIN)
/**
Sets the Obj-C reference for the instance object within the UIView extension.
- Parameter base: Base object.
- Parameter key: Memory key pointer.
- Parameter value: The object instance to set for the associated object.
- Returns: The associated reference for the initializer object.
*/
public static func set<T: Any>(base: Any, key: UnsafePointer<UInt8>, value: T) {
objc_setAssociatedObject(base, key, value, .OBJC_ASSOCIATION_RETAIN)
}
}
......@@ -134,12 +134,12 @@ extension UINavigationItem {
/// NavigationItem reference.
public internal(set) var navigationItem: NavigationItem {
get {
return AssociatedObject(base: self, key: &NavigationItemKey) {
return AssociatedObject.get(base: self, key: &NavigationItemKey) {
return NavigationItem()
}
}
set(value) {
AssociateObject(base: self, key: &NavigationItemKey, value: value)
AssociatedObject.set(base: self, key: &NavigationItemKey, value: value)
}
}
......
......@@ -29,6 +29,7 @@
*/
import UIKit
import Motion
/// A memory reference to the TabsBarItem instance for UIViewController extensions.
fileprivate var TabsBarItemKey: UInt8 = 0
......@@ -51,12 +52,12 @@ extension UIViewController {
/// pageMenuBarItem reference.
public private(set) var pageMenuBarItem: TabsBarItem {
get {
return AssociatedObject(base: self, key: &TabsBarItemKey) {
return AssociatedObject.get(base: self, key: &TabsBarItemKey) {
return TabsBarItem()
}
}
set(value) {
AssociateObject(base: self, key: &TabsBarItemKey, value: value)
AssociatedObject.set(base: self, key: &TabsBarItemKey, value: value)
}
}
}
......@@ -248,6 +249,7 @@ extension TabsController {
for v in viewControllers {
let button = v.pageMenuBarItem as UIButton
v.isMotionEnabled = true
buttons.append(button)
}
......@@ -472,6 +474,8 @@ extension TabsController {
// prepareViewControllersForTransition(from: selectedIndex, to: i)
selectedIndex = i
v.select(at: i)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment