Skip to content

Instantly share code, notes, and snippets.

@saoudrizwan
Last active March 12, 2017 07:26
Show Gist options
  • Save saoudrizwan/aecea5038d22654f2cb05c0786c4521a to your computer and use it in GitHub Desktop.
Save saoudrizwan/aecea5038d22654f2cb05c0786c4521a to your computer and use it in GitHub Desktop.
// http://nshipster.com/swift-objc-runtime/
extension UIViewController {
private struct AssociatedKeys {
static var DescriptiveName = "nsh_DescriptiveName"
}
var descriptiveName: String? {
get {
return objc_getAssociatedObject(self, &AssociatedKeys.DescriptiveName) as? String
}
set {
if let newValue = newValue {
objc_setAssociatedObject(
self,
&AssociatedKeys.DescriptiveName,
newValue as NSString?,
.OBJC_ASSOCIATION_RETAIN_NONATOMIC
)
}
}
}
}
// https://github.com/chbo297/CCZoomTransition/blob/master/CCZoomTransition/CCZoomTransitionVCExtension.swift
extension UIViewController {
private struct AssociatedKey {
static var ZoomTransitioner = "cc_zoomTransitioner"
}
private var cc_transitioner : CCZoomTransitioner?{
get {
if let transitioner = objc_getAssociatedObject(self, &AssociatedKey.ZoomTransitioner) as? CCZoomTransitioner {
return transitioner
} else {
return nil
}
}
set {
objc_setAssociatedObject(self, &AssociatedKey.ZoomTransitioner, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
// nonatomic - fast, not thread safe. atomic - deafult behavior, thread safe.
// http://stackoverflow.com/a/10753201/3502608
}
}
}
extension UIView {
// public var dance: Dance {
// get {
// return DanceExtensionStoredPropertyHandler.associatedObject(base: self, key: &DanceExtensionStoredPropertyHandler.danceKey) {
// return Dance(dancingView: self) // initial value - is reference type so it won't change
// }
// }
// set {
// DanceExtensionStoredPropertyHandler.associateObject(base: self, key: &DanceExtensionStoredPropertyHandler.danceKey, value: newValue)
// }
// }
fileprivate struct DanceAssociatedKey {
static var dance = "dance_key"
}
public var dance: Dance {
get {
if let danceInstance = objc_getAssociatedObject(self, &DanceAssociatedKey.dance) as? Dance {
return danceInstance
} else {
let newDanceInstance = Dance(dancingView: self)
objc_setAssociatedObject(self, &DanceAssociatedKey.dance, newDanceInstance, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
return newDanceInstance
}
}
// set {
// objc_setAssociatedObject(self, &DanceAssociatedKey.dance, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
// }
}
}
// Bool
fileprivate struct PushMenuAssociatedKey {
static var isEnabledKey = "push_menu_enabled_key"
}
public var isPushMenuEnabled: Bool {
get {
if let isPushMenuEnabledInstance = objc_getAssociatedObject(self, &PushMenuAssociatedKey.isEnabledKey) as? Bool {
return isPushMenuEnabledInstance
} else {
let newIsPushMenuEnabledInstance = false
objc_setAssociatedObject(self, &PushMenuAssociatedKey.isEnabledKey, newIsPushMenuEnabledInstance, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
return newIsPushMenuEnabledInstance
}
}
set {
objc_setAssociatedObject(self, &PushMenuAssociatedKey.isEnabledKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
if newValue {
// ...
} else {
// ...
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment