以多种方式构造和配置您的实体。适用于结构和类
Person
struct Person {
var name: String = ""
var age: Int = 0
var website: NSURL?
init() {
}
}
extension Person: Initable {}
let person: Person = construct {
$0.name = "Luffy"
$0.age = 17
}
XCTAssertEqual(person.name, "Luffy")
XCTAssertEqual(person.age, 17)
XCTAssertNil(person.website)
var person = Person() // Declare as `var`
build(&person) { // Use `&`
$0.name = "Luffy"
$0.age = 17
}
XCTAssertEqual(person.name, "Luffy")
XCTAssertEqual(person.age, 17)
XCTAssertNil(person.website)
Car
class Car {
var model: String = ""
var price: Int = 0
}
extension Car: Configurable {}
let car = build(Car()) {
$0.model = "Tesla Model 3"
$0.price = 35_000
}
XCTAssertEqual(car.model, "Tesla Model 3")
XCTAssertEqual(car.price, 35_000)
let car = Car().configure {
$0.model = "Tesla Model 3"
$0.price = 35_000
}
XCTAssertEqual(car.model, "Tesla Model 3")
XCTAssertEqual(car.price, 35_000)
Construction 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中:
pod 'Construction'
Construction 也通过 Carthage 提供。要安装,只需将以下内容写入您的 Cartfile 中:
github "onmyway133/Construction"
Khoa Pham, [email protected]
我们非常愿意您为 Construction 做出贡献,请查看 CONTRIBUTING 文件以获取更多信息。
Construction 在 MIT 许可证下提供。有关更多信息,请参阅 LICENSE 文件。