-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Sergey Makeev edited this page Sep 28, 2019
·
8 revisions
SwiftWhen is a lib for iOS to support expression like functional casting between types.
To use add lib to your project and add
import SwiftWhenor use CocoaPods:
pod "SomeWhen"
import SomeWhenor just add SwiftWhen.swift directly to your project.
It provides several ways.
When is a class providing interface similar to switch operator but with possibility to be used in expression.
Example:
let source = 5
let result = When(.simple, source) {
$0(0) => "Zero"
$0(1) => "One"
$0(2) => "Two"
$0(3) => "Three"
$0(false) => "Four"
$0(true) => {
return "Five"
}()
$0(6) => "Six"
$0(7) => "Seven"
$0(8) => "Eight"
$0(9) => "Nine"
}.default("default")Another variant:
let a1 = 2
let b1 = When(.simple, a1) {
$0(0) => "Zero"
$0(1) => "One"
$0(2) => .skip
$0(3) => .skip
$0(4) => .skip
$0(5) => "Five"
$0(6) => "Six"
$0(7) => "Seven"
$0(8) => "Eight"
$0(9) => "Nine"
}.default(nil) ?? "Unknown"let result = When(number)
.case({$0 < 0}) {"Number should be > 0"}
.case(0) {"Invalid number"}
.case(1)
.case(2) {"Number is too low"}
.case(3) {"Number is correct"}
.case(4) {"Numbe is almost correct"}
.default(nil) ?? "Number is too high"Here we have a String by Int.
When for more details.
If you have a rule how to create one type from another you may create a new function to be used in an expression or use when function which simply wraps this operation inside it's handler
let result = when(source) {
//Here could be any culculations
let resultOfCulculations = calculations()
return resultOfCulculations
}More functions in example:
class A1 {
var str: A2 = A2()
class A2 {
var str: A3 = A3()
class A3 {
var str: String = "A3"
}
}
}
func testWith() {
let a = A1()
with(a.str.str) {
XCTAssert($0.str == "A3")
}
}
func testLet() {
let a: A1? = A1()
let str = a.let {
$0.str.str.str
}
XCTAssert(str == "A3")
}
func testRunMethod() {
let a: A1? = A1()
a.run {
let str = $0.str.str.str
XCTAssert(str == "A3")
}
}
func testApply() {
let a: A1? = A1()
_ = a?.str.str.str
a.apply {
$0.str.str.str = "New A3"
}
a.map {
XCTAssert($0.str.str.str == "New A3")
}
a.apply {
$0.str.str.str = "first iteration"
}.apply {
$0.str.str.str = "second iteration"
}.apply {
$0.str.str.str = "third iteration"
}
a.map {
XCTAssert($0.str.str.str == "third iteration")
}
}
func testTakeWhen() {
var a: Int? = 2
if let validA = a.takeWhen({$0 > 1}) {
XCTAssert(validA == 2)
} else {
XCTAssert(false)
}
a = 1
if let _ = a.takeWhen({$0 > 1}) {
XCTAssert(false)
} else {
XCTAssert(a == 1)
}
a = nil
if let _ = a.takeWhen({$0 > 1}) {
XCTAssert(false)
} else {
XCTAssert(a == nil)
}
a = 100
if let _ = a.takeWhen({$0 > 1}) {
XCTAssert(a == 100)
} else {
XCTAssert(false)
}
if let validA = a.takeWhen({$0 > 1}).takeWhen({$0 == 100}) {
XCTAssert(validA == 100)
} else {
XCTAssert(false)
}
}when for more details.
let result = state == .finished => { 0 } =>? {
state == .working || state == .pausing => { 1 } =>? {
state == .idle || state == .preparing || state == .ready => { 2 } =>! {
3
}
}
}Operators for more details.