目录
目录README.md

HandyOperators

Provides some general-purpose operators for shorter & more expressive code.

<- (“with”)

Applies a closure to a value, returning the transformed copy.

final class Client {
    // with HandyOperators
    let jsonDecoder1 = JSONDecoder() <- {
        $0.dateDecodingStrategy = .iso8601
    }
    
    // without HandyOperators
    let jsonDecoder2: JSONDecoder = {
        let jsonDecoder = JSONDecoder()
        jsonDecoder.dateDecodingStrategy = .iso8601
        return jsonDecoder
    }()
}

Also useful for easily applying mutating functions to copies:

extension Array {
    // with HandyOperators
    func appending1(_ element: Element) -> Self {
        self <- { $0.append(element) }
    }
    
    // without HandyOperators
    func appending2(_ element: Element) -> Self {
        var copy = self
        copy.append(element)
        return copy
    }
}

??? (unwrap or throw)

Takes an optional and an error (autoclosure, so it’s lazy) and returns the unwrapped optional or throws if nil.

// with HandyOperators
func doSomething1(with optional: String?) throws {
    print(try optional ??? MissingValueError())
}

// without HandyOperators
func doSomething2(with optional: String?) throws {
    guard let value = optional else { throw MissingValueError() }
    print(value)
}
关于
40.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

©Copyright 2023 CCF 开源发展委员会
Powered by Trustie& IntelliDE 京ICP备13000930号