Skip to content

Commit 7e5756e

Browse files
committed
Added '<>' (swap) operator
1 parent 5a76447 commit 7e5756e

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed

Playground.playground/Pages/Summary.xcplaygroundpage/Contents.swift

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
**Property Operators**
2121
- [`ping()`](Ping) : Pings a MutableProperty
2222
- [`toggle()`](Toggle) : Toggles a MutableProperty of Bool
23+
- [`<> (swap)`](Swap) : Swaps a new value
2324

2425
**Signal Operators**
2526
- [`combine(withLatest:)`](CombineWithLatest) : Combines value and latest value of another Signal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*:
2+
## `<> (swap)`
3+
Swaps new value
4+
*/
5+
6+
import ReactiveSwift
7+
import ReactiveSwifty
8+
9+
10+
/// Atomic
11+
let atomic = Atomic<Bool>(false)
12+
13+
atomic <> true
14+
atomic <> true
15+
16+
17+
/// MutableProperty
18+
let property = MutableProperty<Bool>(false)
19+
20+
property <> true
21+
property <> true
22+
23+
24+
/// MutableActionProperty
25+
let actionProperty = MutableActionProperty<Bool>()
26+
27+
actionProperty <> true
28+
actionProperty <> true
29+
30+
31+
//: < [Summary](Summary) | [Next](@next) >

Playground.playground/contents.xcplayground

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<page name='MutableActionProperty'/>
77
<page name='Ping'/>
88
<page name='Toggle'/>
9+
<page name='Swap'/>
910
<page name='CombineWithLatest'/>
1011
<page name='ConcatMap'/>
1112
<page name='FilterWithLatest'/>

ReactiveSwifty.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
82AB2B67219E4E6000987CCB /* CombineWith.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82AB2B66219E4E6000987CCB /* CombineWith.swift */; };
4343
82AB2B69219E5AA200987CCB /* SkipError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82AB2B68219E5AA200987CCB /* SkipError.swift */; };
4444
82AB2B6B219E5B8900987CCB /* SkipError_Spec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82AB2B6A219E5B8900987CCB /* SkipError_Spec.swift */; };
45+
82C6B1D1222738AC00413DC3 /* Swap.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82C6B1D0222738AC00413DC3 /* Swap.swift */; };
4546
82DEA54F21FD0BBA00CA6514 /* MutableActionProperty.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82DEA54E21FD0BBA00CA6514 /* MutableActionProperty.swift */; };
4647
915B672D101EEED88AD82137 /* Pods_ReactiveSwifty_iOSTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B9CFEA987BFADCE581878ED1 /* Pods_ReactiveSwifty_iOSTests.framework */; };
4748
/* End PBXBuildFile section */
@@ -106,6 +107,7 @@
106107
82AB2B66219E4E6000987CCB /* CombineWith.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CombineWith.swift; sourceTree = "<group>"; };
107108
82AB2B68219E5AA200987CCB /* SkipError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SkipError.swift; sourceTree = "<group>"; };
108109
82AB2B6A219E5B8900987CCB /* SkipError_Spec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SkipError_Spec.swift; sourceTree = "<group>"; };
110+
82C6B1D0222738AC00413DC3 /* Swap.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Swap.swift; sourceTree = "<group>"; };
109111
82DEA54E21FD0BBA00CA6514 /* MutableActionProperty.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MutableActionProperty.swift; sourceTree = "<group>"; };
110112
98031A09842BCE0AF7DB96FE /* Pods-ReactiveSwifty-iOSTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ReactiveSwifty-iOSTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-ReactiveSwifty-iOSTests/Pods-ReactiveSwifty-iOSTests.release.xcconfig"; sourceTree = "<group>"; };
111113
B9CFEA987BFADCE581878ED1 /* Pods_ReactiveSwifty_iOSTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_ReactiveSwifty_iOSTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -239,6 +241,7 @@
239241
children = (
240242
825DCF76219E5EFD00B1F103 /* Ping.swift */,
241243
8267FF4721979B01000F61DA /* Toggle.swift */,
244+
82C6B1D0222738AC00413DC3 /* Swap.swift */,
242245
);
243246
path = "Property Operators";
244247
sourceTree = "<group>";
@@ -495,6 +498,7 @@
495498
82AB2B60219E245F00987CCB /* SpaceOut.swift in Sources */,
496499
8267FF3B21977759000F61DA /* Errors.swift in Sources */,
497500
825DCF7D219E654800B1F103 /* Values.swift in Sources */,
501+
82C6B1D1222738AC00413DC3 /* Swap.swift in Sources */,
498502
8267FF2A2197597F000F61DA /* LifetimeProvider.swift in Sources */,
499503
8267FF432197934B000F61DA /* Completes.swift in Sources */,
500504
8267FF352197736D000F61DA /* ConcatMap.swift in Sources */,

Sources/Property Operators/Swap.swift

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// Swap.swift
3+
// ReactiveSwifty-iOS
4+
//
5+
// Created by José Donor on 27/02/2019.
6+
// Copyright © 2019 iDonJose. All rights reserved.
7+
//
8+
9+
import ReactiveSwift
10+
11+
12+
13+
infix operator <>: AssignmentPrecedence
14+
15+
16+
/// Swaps new value
17+
@discardableResult
18+
public func <> <Value>(property: Atomic<Value>, value: Value) -> Value {
19+
return property.swap(value)
20+
}
21+
22+
/// Swaps new value
23+
@discardableResult
24+
public func <> <Value>(property: MutableProperty<Value>, value: Value) -> Value {
25+
return property.swap(value)
26+
}
27+
28+
/// Swaps new value
29+
@discardableResult
30+
public func <> <Value>(property: MutableActionProperty<Value>, value: Value) -> Value? {
31+
return property.swap(value)
32+
}

0 commit comments

Comments
 (0)