From 82bccfdf4a3bd859d76bd2d576cfebb4d946a146 Mon Sep 17 00:00:00 2001 From: David Rodrigues Date: Sat, 2 Jul 2016 10:36:47 +0100 Subject: [PATCH 1/2] Changed `rex_text` bindable property to be an optional string Fixes #125, #129. --- Source/UIKit/UILabel.swift | 5 +++-- Tests/UIKit/UILabelTests.swift | 2 +- Tests/UIKit/UITableViewCellTests.swift | 1 + 3 files changed, 5 insertions(+), 3 deletions(-) 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..49d3c98 100644 --- a/Tests/UIKit/UILabelTests.swift +++ b/Tests/UIKit/UILabelTests.swift @@ -36,7 +36,7 @@ class UILabelTests: XCTestCase { label.text = "" let (pipeSignal, observer) = Signal.pipe() - label.rex_text <~ SignalProducer(signal: pipeSignal) + label.rex_text <~ SignalProducer(signal: pipeSignal).producer.map(Optional.init) // TODO: Remove in the future, binding with optionals will be available soon in RAC observer.sendNext(firstChange) XCTAssertEqual(label.text, firstChange) diff --git a/Tests/UIKit/UITableViewCellTests.swift b/Tests/UIKit/UITableViewCellTests.swift index 72eba7b..6304fe1 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 .takeUntil(cell.rex_prepareForReuse) XCTAssertEqual(label.text, "John") From 50f06bd1dc93b3d5aba0f9afc116f00b457c252b Mon Sep 17 00:00:00 2001 From: David Rodrigues Date: Mon, 4 Jul 2016 22:58:22 +0100 Subject: [PATCH 2/2] Updated UILabel's text property test to include nullability --- Tests/UIKit/UILabelTests.swift | 6 ++++-- Tests/UIKit/UITableViewCellTests.swift | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Tests/UIKit/UILabelTests.swift b/Tests/UIKit/UILabelTests.swift index 49d3c98..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() - label.rex_text <~ SignalProducer(signal: pipeSignal).producer.map(Optional.init) // TODO: Remove in the future, binding with optionals will be available soon in RAC + 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 6304fe1..d08767f 100644 --- a/Tests/UIKit/UITableViewCellTests.swift +++ b/Tests/UIKit/UITableViewCellTests.swift @@ -24,7 +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 + .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")