diff --git a/Rex.xcodeproj/project.pbxproj b/Rex.xcodeproj/project.pbxproj index 203c3be..f2ccfb8 100644 --- a/Rex.xcodeproj/project.pbxproj +++ b/Rex.xcodeproj/project.pbxproj @@ -8,6 +8,8 @@ /* Begin PBXBuildFile section */ 4238D5961B4D5950008534C0 /* NSTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4238D5951B4D5950008534C0 /* NSTextField.swift */; }; + 7D2AA99B1CB6EFEB008AB5C9 /* UISwitch.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2AA99A1CB6EFEB008AB5C9 /* UISwitch.swift */; }; + 7D2AA99D1CB6F275008AB5C9 /* UISwitchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2AA99C1CB6F275008AB5C9 /* UISwitchTests.swift */; }; 8289A2E11BD7EF1F0097FB60 /* UIImageViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8289A2E01BD7EF1F0097FB60 /* UIImageViewTests.swift */; }; 8289A2E31BD7EF740097FB60 /* UIImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8289A2E21BD7EF740097FB60 /* UIImageView.swift */; }; 8289A2E51BD7F6DD0097FB60 /* UIView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8289A2E41BD7F6DD0097FB60 /* UIView.swift */; }; @@ -173,6 +175,8 @@ 4238D5951B4D5950008534C0 /* NSTextField.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = NSTextField.swift; path = AppKit/NSTextField.swift; sourceTree = ""; }; 5173EBC51B625A2600C9B48E /* UIBarItem.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIBarItem.swift; sourceTree = ""; }; 5173EBC71B625A6800C9B48E /* UIBarButtonItem.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIBarButtonItem.swift; sourceTree = ""; }; + 7D2AA99A1CB6EFEB008AB5C9 /* UISwitch.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UISwitch.swift; sourceTree = ""; }; + 7D2AA99C1CB6F275008AB5C9 /* UISwitchTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UISwitchTests.swift; sourceTree = ""; }; 8289A2E01BD7EF1F0097FB60 /* UIImageViewTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIImageViewTests.swift; sourceTree = ""; }; 8289A2E21BD7EF740097FB60 /* UIImageView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIImageView.swift; sourceTree = ""; }; 8289A2E41BD7F6DD0097FB60 /* UIView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIView.swift; sourceTree = ""; }; @@ -393,6 +397,7 @@ 9DA915A31CA6301C003723B9 /* UIDatePicker.swift */, 8289A2E21BD7EF740097FB60 /* UIImageView.swift */, D86FFBD71B34B242001A89B3 /* UILabel.swift */, + 7D2AA99A1CB6EFEB008AB5C9 /* UISwitch.swift */, C7932E811C4B3EDB00086F3C /* UITextField.swift */, 8289A2E41BD7F6DD0097FB60 /* UIView.swift */, ); @@ -429,6 +434,7 @@ 9DA915A51CA63046003723B9 /* UIDatePickerTests.swift */, 8289A2E01BD7EF1F0097FB60 /* UIImageViewTests.swift */, D8F073141B861B3A0047D546 /* UILabelTests.swift */, + 7D2AA99C1CB6F275008AB5C9 /* UISwitchTests.swift */, C7932E851C4B420A00086F3C /* UITextFieldTests.swift */, 8289A2E61BD7F7730097FB60 /* UIViewTests.swift */, ); @@ -770,6 +776,7 @@ D8F0973E1B17F30D002E15BA /* NSUserDefaults.swift in Sources */, D834572D1AFEE45B0070616A /* Signal.swift in Sources */, D8E4A6211B7BBB2100EAD8A8 /* UIBarItem.swift in Sources */, + 7D2AA99B1CB6EFEB008AB5C9 /* UISwitch.swift in Sources */, D8A454071BD26A1A00C9E790 /* Property.swift in Sources */, D8E4A6201B7BBB1600EAD8A8 /* UIBarButtonItem.swift in Sources */, D8F097451B17F3C8002E15BA /* NSObject.swift in Sources */, @@ -789,6 +796,7 @@ D8F0974B1B17F5E2002E15BA /* NSObjectTests.swift in Sources */, 8289A2E81BD7F7900097FB60 /* UIViewTests.swift in Sources */, D8F073161B863CE70047D546 /* UILabelTests.swift in Sources */, + 7D2AA99D1CB6F275008AB5C9 /* UISwitchTests.swift in Sources */, C7932E871C4B42F500086F3C /* UITextFieldTests.swift in Sources */, 8295FD8A1B87352D007C9000 /* UIButtonTests.swift in Sources */, D8A4540A1BD2772700C9E790 /* PropertyTests.swift in Sources */, diff --git a/Source/UIKit/UISwitch.swift b/Source/UIKit/UISwitch.swift new file mode 100644 index 0000000..c72916a --- /dev/null +++ b/Source/UIKit/UISwitch.swift @@ -0,0 +1,29 @@ +// +// UISwitch.swift +// Rex +// +// Created by David Rodrigues on 07/04/16. +// Copyright © 2016 Neil Pankey. All rights reserved. +// + +import ReactiveCocoa +import UIKit + +extension UISwitch { + + /// Wraps a switch's `on` state in a bindable property. + public var rex_on: MutableProperty { + + let property = associatedProperty(self, key: &onKey, initial: { $0.on }, setter: { $0.on = $1 }) + + property <~ rex_controlEvents(.ValueChanged) + .map { $0 as? UISwitch } + .ignoreNil() + .map { $0.on } + + return property + } + +} + +private var onKey: UInt8 = 0 diff --git a/Tests/UIKit/UISwitchTests.swift b/Tests/UIKit/UISwitchTests.swift new file mode 100644 index 0000000..bc3c1ef --- /dev/null +++ b/Tests/UIKit/UISwitchTests.swift @@ -0,0 +1,28 @@ +// +// UISwitchTests.swift +// Rex +// +// Created by David Rodrigues on 07/04/16. +// Copyright © 2016 Neil Pankey. All rights reserved. +// + +import XCTest +import ReactiveCocoa +import Result + +class UISwitchTests: XCTestCase { + + func testOnProperty() { + let s = UISwitch(frame: CGRectZero) + s.on = false + + let (pipeSignal, observer) = Signal.pipe() + s.rex_on <~ SignalProducer(signal: pipeSignal) + + observer.sendNext(true) + XCTAssertTrue(s.on) + observer.sendNext(false) + XCTAssertFalse(s.on) + } + +}