Appliable logo

Build Code coverage Language Supported platforms

Swift Package Manager Release version License

Appliable

Appliable 使用闭包使配置对象更加容易和方便。

目录
  1. 亮点
  2. 安装
  3. 示例
  4. 遵循
  5. 许可

亮点

let button = UIButton().apply {
    $0.isUserInteractionEnabled = false
}
UserDefaults.standard.apply {
    $0.set("Value 1", forKey: "Key 1")
    $0.set("Value 2", forKey: "Key 2")
    $0.set("Value 3", forKey: "Key 3")
}

安装

要求

Swift Package Manager

Appliable 可以通过 Swift Package 方式获取。

.package(url: "https://github.com/pawello2222/Appliable.git", .upToNextMajor(from: "1.0.0"))

示例

let button = UIButton().apply {
    $0.isUserInteractionEnabled = false
}
let label = UILabel()

label.apply {
    $0.isUserInteractionEnabled = false
}

结构体

let calendar = Calendar(identifier: .gregorian).applying {
    $0.timeZone = .init(identifier: "UTC")!
    $0.locale = .init(identifier: "en_US_POSIX")
}
var calendar = Calendar(identifier: .gregorian)

calendar.apply {
    $0.timeZone = .init(identifier: "UTC")!
    $0.locale = .init(identifier: "en_US_POSIX")
}

Block 操作

UserDefaults.standard.apply {
    $0.set("Value 1", forKey: "Key 1")
    $0.set("Value 2", forKey: "Key 2")
    $0.set("Value 3", forKey: "Key 3")
}

数组

var array = Array(repeating: Date(), count: 3)

array.applyEach {
    $0.addTimeInterval(1000)
}
let array1 = Array(repeating: Date(), count: 3)

let array2 = array1.applyingEach {
    $0.addTimeInterval(1000)
}
let label = UILabel()
let button = UIButton()

[label, button].applyEach {
    $0.isUserInteractionEnabled = false
}

let components = [UILabel(), UIButton()].applyEach {
    $0.isUserInteractionEnabled = false
}

遵循

内置遵循

  1. 值类型
  1. 引用类型
  1. 所有继承自 NSObject 的类

自定义遵循

extension Calendar: Appliable {} // value types

extension JSONDecoder: ObjectAppliable {} // reference types
struct Item: Appliable {
    var value: Int?
}

let item = Item().applying {
    $0.value = 1
}

许可

Appliable 基于 MIT 许可发布。更多信息请查看 LICENSE 文件。