Last active
March 21, 2016 09:11
-
-
Save IamAlchemist/61e14053785166de6270 to your computer and use it in GitHub Desktop.
RAC.swift
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
// | |
// RAC.swift | |
// MapleStory | |
// | |
// Created by Wizard Li on 3/21/16. | |
// Copyright © 2016 tencent. All rights reserved. | |
// | |
// Original source can be found at: | |
// https://github.com/ColinEberhardt/ReactiveTwitterSearch/blob/master/ReactiveTwitterSearch/Util/UIKitExtensions.swift | |
// | |
import Foundation | |
import ReactiveCocoa | |
import UIKit | |
// see https://github.com/ReactiveCocoa/ReactiveCocoa/issues/2704 | |
import enum Result.NoError | |
public typealias NoError = Result.NoError | |
struct AssociationKey { | |
static var hidden: UInt8 = 1 | |
static var date: UInt8 = 2 | |
static var text: UInt8 = 3 | |
} | |
func lazyAssociatedProperty<T: AnyObject>(host: AnyObject, key: UnsafePointer<Void>, factory: ()->T) -> T { | |
return objc_getAssociatedObject(host, key) as? T ?? { | |
let associatedProperty = factory() | |
objc_setAssociatedObject(host, key, associatedProperty, .OBJC_ASSOCIATION_RETAIN) | |
return associatedProperty | |
}() | |
} | |
func lazyMutableProperty<T>(host: AnyObject, key: UnsafePointer<Void>, setter: T -> (), getter: () -> T) -> MutableProperty<T> { | |
return lazyAssociatedProperty(host, key: key) { | |
let property = MutableProperty<T>(getter()) | |
property.producer | |
.startWithNext { | |
newValue in | |
setter(newValue) | |
} | |
return property | |
} | |
} | |
extension UIView { | |
public var rac_hidden: MutableProperty<Bool> { | |
return lazyMutableProperty(self, key: &AssociationKey.hidden, setter: { self.hidden = $0 }, getter: { self.hidden }) | |
} | |
} | |
extension UILabel { | |
public var rac_text: MutableProperty<String> { | |
return lazyMutableProperty(self, key: &AssociationKey.text, setter: {[unowned self] in self.text = $0 }, getter: {[unowned self] in self.text ?? "" }) | |
} | |
} | |
extension UITextField { | |
func rac_textSignalProducer() -> SignalProducer<String, NoError> { | |
return self.rac_textSignal().toSignalProducer() | |
.map { $0 as! String } | |
.flatMapError { _ in SignalProducer<String, NoError>.empty } | |
} | |
} | |
extension UITextField { | |
public var rac_text: MutableProperty<String> { | |
return lazyAssociatedProperty(self, key: &AssociationKey.text) { | |
self.addTarget(self, action: "changed", forControlEvents: UIControlEvents.EditingChanged) | |
let property = MutableProperty<String>(self.text ?? "") | |
property.producer | |
.startWithNext { newValue in | |
self.text = newValue | |
} | |
return property | |
} | |
} | |
func changed() { | |
rac_text.value = self.text! | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment