From fd11dcd4a5e3ba672788b0e10efc8ec1450df807 Mon Sep 17 00:00:00 2001 From: Patrick Reynolds Date: Thu, 10 Mar 2016 09:28:23 -0800 Subject: [PATCH] Remove implicitly unwrapped optionals in Observer and OnceToken getters --- .../Demo/EasyPull/UIScrollView+EasyPull.swift | 20 ++++++++++--------- EasyPull/EasyPull/UIScrollView+EasyPull.swift | 20 ++++++++++--------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/Demo/Demo/EasyPull/UIScrollView+EasyPull.swift b/Demo/Demo/EasyPull/UIScrollView+EasyPull.swift index 0b005da..239788f 100644 --- a/Demo/Demo/EasyPull/UIScrollView+EasyPull.swift +++ b/Demo/Demo/EasyPull/UIScrollView+EasyPull.swift @@ -17,12 +17,13 @@ extension UIScrollView { // MARK: - constant and veriable and property private var Observer: EasyObserver { get { - var obj = objc_getAssociatedObject(self, &AssociatedKeys.ContentOffsetObserver) as? EasyObserver - if obj == nil { - obj = EasyObserver(scrollView: self) + if let obj = objc_getAssociatedObject(self, &AssociatedKeys.ContentOffsetObserver) as? EasyObserver { + return obj + } else { + let obj = EasyObserver(scrollView: self) objc_setAssociatedObject(self, &AssociatedKeys.ContentOffsetObserver, obj, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) + return obj } - return obj! } set { objc_setAssociatedObject(self, &AssociatedKeys.ContentOffsetObserver, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) @@ -31,18 +32,19 @@ extension UIScrollView { private var OnceToken: dispatch_once_t { get { - var token = objc_getAssociatedObject(self, &AssociatedKeys.OnceToken) as? dispatch_once_t - if token == nil { - token = 0 + if let token = objc_getAssociatedObject(self, &AssociatedKeys.OnceToken) as? dispatch_once_t { + return token + } else { + let token = 0 objc_setAssociatedObject(self, &AssociatedKeys.OnceToken, token, .OBJC_ASSOCIATION_ASSIGN) + return token } - return token! } set { objc_setAssociatedObject(self, &AssociatedKeys.OnceToken, newValue, .OBJC_ASSOCIATION_ASSIGN) } } - + // MARK: - public method /** add drop pull to refresh diff --git a/EasyPull/EasyPull/UIScrollView+EasyPull.swift b/EasyPull/EasyPull/UIScrollView+EasyPull.swift index 0b005da..239788f 100644 --- a/EasyPull/EasyPull/UIScrollView+EasyPull.swift +++ b/EasyPull/EasyPull/UIScrollView+EasyPull.swift @@ -17,12 +17,13 @@ extension UIScrollView { // MARK: - constant and veriable and property private var Observer: EasyObserver { get { - var obj = objc_getAssociatedObject(self, &AssociatedKeys.ContentOffsetObserver) as? EasyObserver - if obj == nil { - obj = EasyObserver(scrollView: self) + if let obj = objc_getAssociatedObject(self, &AssociatedKeys.ContentOffsetObserver) as? EasyObserver { + return obj + } else { + let obj = EasyObserver(scrollView: self) objc_setAssociatedObject(self, &AssociatedKeys.ContentOffsetObserver, obj, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) + return obj } - return obj! } set { objc_setAssociatedObject(self, &AssociatedKeys.ContentOffsetObserver, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) @@ -31,18 +32,19 @@ extension UIScrollView { private var OnceToken: dispatch_once_t { get { - var token = objc_getAssociatedObject(self, &AssociatedKeys.OnceToken) as? dispatch_once_t - if token == nil { - token = 0 + if let token = objc_getAssociatedObject(self, &AssociatedKeys.OnceToken) as? dispatch_once_t { + return token + } else { + let token = 0 objc_setAssociatedObject(self, &AssociatedKeys.OnceToken, token, .OBJC_ASSOCIATION_ASSIGN) + return token } - return token! } set { objc_setAssociatedObject(self, &AssociatedKeys.OnceToken, newValue, .OBJC_ASSOCIATION_ASSIGN) } } - + // MARK: - public method /** add drop pull to refresh