Skip to content

Commit

Permalink
Merge pull request #65 from neilpa/key-shadowing
Browse files Browse the repository at this point in the history
Fix associated object key shadowing
  • Loading branch information
neilpa committed Nov 22, 2015
2 parents ef8d2b1 + a295a1a commit 8386f86
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 22 deletions.
4 changes: 2 additions & 2 deletions Source/UIKit/UIBarButtonItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extension UIBarButtonItem {
/// overwritten. This also binds the enabled state of the action to the `rex_enabled`
/// property on the button.
public var rex_action: MutableProperty<CocoaAction> {
return associatedObject(self, key: &action) { [weak self] _ in
return associatedObject(self, key: &actionKey) { [weak self] _ in
let initial = CocoaAction.rex_disabled
let property = MutableProperty(initial)

Expand All @@ -33,4 +33,4 @@ extension UIBarButtonItem {
}
}

private var action: UInt8 = 0
private var actionKey: UInt8 = 0
4 changes: 2 additions & 2 deletions Source/UIKit/UIBarItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import UIKit
extension UIBarItem {
/// Wraps a UIBarItem's `enabled` state in a bindable property.
public var rex_enabled: MutableProperty<Bool> {
return associatedProperty(self, key: &enabled, initial: { [weak self] in self?.enabled ?? true }, setter: { [weak self] in self?.enabled = $0 })
return associatedProperty(self, key: &enabledKey, initial: { [weak self] in self?.enabled ?? true }, setter: { [weak self] in self?.enabled = $0 })
}
}

private var enabled: UInt8 = 0
private var enabledKey: UInt8 = 0
8 changes: 4 additions & 4 deletions Source/UIKit/UIButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extension UIButton {
/// previous action is removed as a target. This also binds the enabled state of the
/// action to the `rex_enabled` property on the button.
public var rex_pressed: MutableProperty<CocoaAction> {
return associatedObject(self, key: &pressed, initial: { [weak self] _ in
return associatedObject(self, key: &pressedKey, initial: { [weak self] _ in
let initial = CocoaAction.rex_disabled
let property = MutableProperty(initial)

Expand All @@ -36,9 +36,9 @@ extension UIButton {
/// Wraps a button's `title` text in a bindable property. Note that this only applies
/// to `UIControlState.Normal`.
public var rex_title: MutableProperty<String> {
return rex_valueProperty(&title, { [weak self] in self?.titleForState(.Normal) ?? "" }, { [weak self] in self?.setTitle($0, forState: .Normal) })
return rex_valueProperty(&titleKey, { [weak self] in self?.titleForState(.Normal) ?? "" }, { [weak self] in self?.setTitle($0, forState: .Normal) })
}
}

private var pressed: UInt8 = 0
private var title: UInt8 = 0
private var pressedKey: UInt8 = 0
private var titleKey: UInt8 = 0
12 changes: 6 additions & 6 deletions Source/UIKit/UIControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ extension UIControl {

/// Wraps a control's `enabled` state in a bindable property.
public var rex_enabled: MutableProperty<Bool> {
return rex_valueProperty(&enabled, { [weak self] in self?.enabled ?? true }, { [weak self] in self?.enabled = $0 })
return rex_valueProperty(&enabledKey, { [weak self] in self?.enabled ?? true }, { [weak self] in self?.enabled = $0 })
}

/// Wraps a control's `selected` state in a bindable property.
public var rex_selected: MutableProperty<Bool> {
return rex_valueProperty(&selected, { [weak self] in self?.selected ?? false }, { [weak self] in self?.selected = $0 })
return rex_valueProperty(&selectedKey, { [weak self] in self?.selected ?? false }, { [weak self] in self?.selected = $0 })
}

/// Wraps a control's `highlighted` state in a bindable property.
public var rex_highlighted: MutableProperty<Bool> {
return rex_valueProperty(&highlighted, { [weak self] in self?.highlighted ?? false }, { [weak self] in self?.highlighted = $0 })
return rex_valueProperty(&highlightedKey, { [weak self] in self?.highlighted ?? false }, { [weak self] in self?.highlighted = $0 })
}
}

private var enabled: UInt8 = 0
private var selected: UInt8 = 0
private var highlighted: UInt8 = 0
private var enabledKey: UInt8 = 0
private var selectedKey: UInt8 = 0
private var highlightedKey: UInt8 = 0
8 changes: 4 additions & 4 deletions Source/UIKit/UIImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import UIKit
extension UIImageView {
/// Wraps a imageView's `image` value in a bindable property.
public var rex_image: MutableProperty<UIImage?> {
return rex_valueProperty(&image, { [weak self] in self?.image }, { [weak self] in self?.image = $0 })
return rex_valueProperty(&imageKey, { [weak self] in self?.image }, { [weak self] in self?.image = $0 })
}

/// Wraps a imageView's `highlightedImage` value in a bindable property.
public var rex_highlightedImage: MutableProperty<UIImage?> {
return rex_valueProperty(&highlightedImage, { [weak self] in self?.highlightedImage }, { [weak self] in self?.highlightedImage = $0 })
return rex_valueProperty(&highlightedImageKey, { [weak self] in self?.highlightedImage }, { [weak self] in self?.highlightedImage = $0 })
}
}

private var image: UInt8 = 0
private var highlightedImage: UInt8 = 0
private var imageKey: UInt8 = 0
private var highlightedImageKey: UInt8 = 0
8 changes: 4 additions & 4 deletions Source/UIKit/UIView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import UIKit
extension UIView {
/// Wraps a view's `alpha` value in a bindable property.
public var rex_alpha: MutableProperty<CGFloat> {
return rex_valueProperty(&alpha, { [weak self] in self?.alpha ?? 1.0 }, { [weak self] in self?.alpha = $0 })
return rex_valueProperty(&alphaKey, { [weak self] in self?.alpha ?? 1.0 }, { [weak self] in self?.alpha = $0 })
}

/// Wraps a view's `hidden` state in a bindable property.
public var rex_hidden: MutableProperty<Bool> {
return rex_valueProperty(&hidden, { [weak self] in self?.hidden ?? false }, { [weak self] in self?.hidden = $0 })
return rex_valueProperty(&hiddenKey, { [weak self] in self?.hidden ?? false }, { [weak self] in self?.hidden = $0 })
}
}

private var alpha: UInt8 = 0
private var hidden: UInt8 = 0
private var alphaKey: UInt8 = 0
private var hiddenKey: UInt8 = 0
22 changes: 22 additions & 0 deletions Tests/UIKit/UIControlTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,26 @@ class UIControlTests: XCTestCase {
observer.sendNext(false)
XCTAssertFalse(control.highlighted)
}

func testEnabledAndSelectedProperty() {
let control = UIControl(frame: CGRectZero)
control.selected = false
control.enabled = false

let (pipeSignalSelected, observerSelected) = Signal<Bool, NoError>.pipe()
let (pipeSignalEnabled, observerEnabled) = Signal<Bool, NoError>.pipe()
control.rex_selected <~ SignalProducer(signal: pipeSignalSelected)
control.rex_enabled <~ SignalProducer(signal: pipeSignalEnabled)

observerSelected.sendNext(true)
observerEnabled.sendNext(true)
XCTAssertTrue(control.enabled)
XCTAssertTrue(control.selected)
observerSelected.sendNext(false)
XCTAssertTrue(control.enabled)
XCTAssertFalse(control.selected)
observerEnabled.sendNext(false)
XCTAssertFalse(control.enabled)
XCTAssertFalse(control.selected)
}
}

0 comments on commit 8386f86

Please sign in to comment.