diff --git a/Source/UIKit/UILabel.swift b/Source/UIKit/UILabel.swift index 1ec0c25..c47746f 100644 --- a/Source/UIKit/UILabel.swift +++ b/Source/UIKit/UILabel.swift @@ -11,8 +11,8 @@ import UIKit extension UILabel { /// Wraps a label's `text` value in a bindable property. - public var rex_text: MutableProperty { - return associatedProperty(self, keyPath: "text") + public var rex_text: MutableProperty { + return associatedProperty(self, key: &attributedTextKey, initial: { $0.text }, setter: { $0.text = $1 }) } /// Wraps a label's `attributedText` value in a bindable property. @@ -26,5 +26,6 @@ extension UILabel { } } +private var textKey: UInt8 = 0 private var attributedTextKey: UInt8 = 0 private var textColorKey: UInt8 = 0 diff --git a/Tests/UIKit/UILabelTests.swift b/Tests/UIKit/UILabelTests.swift index 8d10099..9356b9f 100644 --- a/Tests/UIKit/UILabelTests.swift +++ b/Tests/UIKit/UILabelTests.swift @@ -35,13 +35,15 @@ class UILabelTests: XCTestCase { let label = UILabel(frame: CGRectZero) label.text = "" - let (pipeSignal, observer) = Signal.pipe() + let (pipeSignal, observer) = Signal.pipe() label.rex_text <~ SignalProducer(signal: pipeSignal) observer.sendNext(firstChange) XCTAssertEqual(label.text, firstChange) observer.sendNext(secondChange) XCTAssertEqual(label.text, secondChange) + observer.sendNext(nil) + XCTAssertNil(label.text) } func testAttributedTextPropertyDoesntCreateRetainCycle() { diff --git a/Tests/UIKit/UITableViewCellTests.swift b/Tests/UIKit/UITableViewCellTests.swift index 72eba7b..d08767f 100644 --- a/Tests/UIKit/UITableViewCellTests.swift +++ b/Tests/UIKit/UITableViewCellTests.swift @@ -24,6 +24,7 @@ class UITableViewCellTests: XCTestCase { label.rex_text <~ titleProperty .producer + .map(Optional.init) // TODO: Remove in the future, binding with optionals will be available soon in RAC 5. Reference: https://github.com/ReactiveCocoa/ReactiveCocoa/pull/2852 .takeUntil(cell.rex_prepareForReuse) XCTAssertEqual(label.text, "John")