Last active
April 6, 2016 22:21
-
-
Save d3signerd/795a9dced39cbee056010d5629d9ca06 to your computer and use it in GitHub Desktop.
Simple Swift NSObject extension adding Device Type, Delay and Wait
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
// | |
// SimpleNSObject.swift | |
// Rise Alarm | |
// | |
// Created by Kellen Styler on 4/3/16. | |
// Copyright (c) 2016 Kellen Styler. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
// MARK: - typealias | |
typealias dispatch_cancelable_closure = (cancel : Bool) -> () | |
extension NSObject { | |
// MARK: - Device | |
private struct DeviceAssociatedKeys { | |
static var iPhone = "iPhone" | |
static var iPhone5 = "iPhone5" | |
static var iPhone6 = "iPhone6" | |
static var iPhone6Plus = "iPhone6Plus" | |
} | |
final var isPhone:Bool { | |
get { | |
guard let value:NSNumber = objc_getAssociatedObject( self, &DeviceAssociatedKeys.iPhone ) as? NSNumber else { | |
let iPhone:Bool = ( UIDevice.currentDevice().userInterfaceIdiom == .Phone ) | |
objc_setAssociatedObject( self, &DeviceAssociatedKeys.iPhone, NSNumber( bool: iPhone ), objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC ) | |
return iPhone.boolValue | |
} | |
return value.boolValue | |
} | |
set { } | |
} | |
final var isPhone5:Bool { | |
get { | |
guard let value:NSNumber = objc_getAssociatedObject( self, &DeviceAssociatedKeys.iPhone5 ) as? NSNumber else { | |
let iPhone5:Bool = isPhone && ( UIScreen.mainScreen().bounds.height == 568 || UIScreen.mainScreen().bounds.width == 568 ) | |
objc_setAssociatedObject( self, &DeviceAssociatedKeys.iPhone5, NSNumber( bool: iPhone5 ), objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC ) | |
return iPhone5.boolValue | |
} | |
return value.boolValue | |
} | |
set { } | |
} | |
final var isPhone6:Bool { | |
get { | |
guard let value:NSNumber = objc_getAssociatedObject( self, &DeviceAssociatedKeys.iPhone6 ) as? NSNumber else { | |
let iPhone6:Bool = isPhone && ( UIScreen.mainScreen().bounds.height == 667 || UIScreen.mainScreen().bounds.width == 667 ) | |
objc_setAssociatedObject( self, &DeviceAssociatedKeys.iPhone6, NSNumber( bool: iPhone6 ), objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC ) | |
return iPhone6.boolValue | |
} | |
return value.boolValue | |
} | |
set { } | |
} | |
final var isPhone6Plus:Bool { | |
get { | |
guard let value:NSNumber = objc_getAssociatedObject( self, &DeviceAssociatedKeys.iPhone6Plus ) as? NSNumber else { | |
let iPhone6Plus:Bool = isPhone && ( UIScreen.mainScreen().bounds.height == 736 || UIScreen.mainScreen().bounds.width == 736 ) | |
objc_setAssociatedObject( self, &DeviceAssociatedKeys.iPhone6Plus, NSNumber( bool: iPhone6Plus ), objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC ) | |
return iPhone6Plus.boolValue | |
} | |
return value.boolValue | |
} | |
set { } | |
} | |
final var isPortrait:Bool { | |
return UIInterfaceOrientationIsPortrait( UIApplication.sharedApplication().statusBarOrientation ) | |
} | |
// MARK: - Class | |
final var className:String { return "\(self.dynamicType)" } | |
// MARK: - Delay & Wait | |
final func cancelDelay( inout closure:dispatch_cancelable_closure? ) { | |
if closure != nil { closure!( cancel: true ) } | |
closure = nil | |
} | |
final func delay( time:NSTimeInterval, closure:()->()) -> dispatch_cancelable_closure? { | |
func dispatch_later( closure:()->() ) { | |
dispatch_after( dispatch_time( DISPATCH_TIME_NOW, Int64( time * Double( NSEC_PER_SEC ) ) ), dispatch_get_main_queue(), closure ) | |
} | |
var closure:dispatch_block_t? = closure | |
var cancelableClosure:dispatch_cancelable_closure? | |
let delayedClosure:dispatch_cancelable_closure = { cancel in | |
if let closure = closure { | |
if ( cancel == false ) { dispatch_async( dispatch_get_main_queue(), closure ); } | |
} | |
closure = nil | |
cancelableClosure = nil | |
} | |
cancelableClosure = delayedClosure | |
dispatch_later { | |
if let delayedClosure = cancelableClosure { delayedClosure( cancel: false ) } | |
} | |
return cancelableClosure; | |
} | |
final func waitWhile( condition:()->Bool?, completion:()->() ) { | |
guard let conditionMet:Bool = condition() else { return } | |
guard !conditionMet else { | |
completion() | |
return | |
} | |
delay( 0.05 ) { self.waitWhile( condition, completion: completion ) } | |
} | |
} | |
// Wait Example | |
/** | |
waitWhile( { [weak self] () -> Bool? in | |
guard let weakSelf = self else { return nil } | |
return // Your condition check | |
}, completion: { [weak self] in | |
// Wait completion code goes here... | |
}) | |
**/ | |
// Delay Example | |
/** | |
private var cancelableDelayClosure:dispatch_cancelable_closure? = nil | |
cancelableDelayClosure = delay( 1.0, closure: { () -> () in | |
// Delay completion code here... | |
}) | |
cancelDelay( &cancelableDelayClosure ) | |
**/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment