Skip to content

Instantly share code, notes, and snippets.

View andyyhope's full-sized avatar

Andyy Hope andyyhope

View GitHub Profile
struct Foo { }
enum Bar {
case hero(Any)
}
let foo = Foo()
let bar = Bar.hero(foo)
if case let .hero(foo) = bar, foo is Foo {
//:
//: UIView Animation Syntax Sugar
//:
//: Created by Andyy Hope on 18/08/2016.
//: Twitter: @andyyhope
//: Medium: Andyy Hope, https://medium.com/@AndyyHope
import UIKit
extension UIView {
//:
//: UserDefaultable.swift
//:
//: Created by Andyy Hope on 18/08/2016.
//: Twitter: @andyyhope
//: Medium: Andyy Hope, https://medium.com/@AndyyHope
import Foundation
// MARK: - Key Namespaceable
// Swift: Typecasing
// Enums as data models?
//
// Author: Andyy Hope
// Twitter: @andyyhope
// Medium: medium.com/@andyyhope
import Foundation
// MARK: - Models
// Swift: Syntax Cheat Codes
// ↑ ↑ ↓ ↓ ← → ← → B A
//
// Author: Andyy Hope
// Twitter: @andyyhope
// Medium: medium.com/@andyyhope
import UIKit
import Foundation
// MARK: - Protocol
public protocol Notifier {
associatedtype Notification: RawRepresentable
}
public extension Notifier where Notification.RawValue == String {
static func addObserver(observer: AnyObject, selector: Selector, notification: Notification) {
let name = nameFor(notification)
NSNotificationCenter.defaultCenter()
.addObserver(observer, selector: selector, name: name, object: nil)
}
static func removeObserver(observer: AnyObject, notification: Notification, object: AnyObject? = nil) {
let name = nameFor(notification)
NSNotificationCenter.defaultCenter()
.removeObserver(observer, name: name, object: object)
}
private static func nameFor(notification: Notification) -> String {
return "\(self).\(notification.rawValue)"
}
static func postNotification(notification: Notification, object: AnyObject? = nil, userInfo: [String : AnyObject]? = nil) {
let name = nameFor(notification)
NSNotificationCenter.defaultCenter()
.postNotificationName(name, object: object, userInfo: userInfo)
}