从 Swift 方法生成可链接的方法代码
修饰基因
从 Swift 方法生成可链接的方法代码。
用法
您可以从Release下载它
USAGE: swchaingen --input-dir <input-dir> --output-dir <output-dir> [--overwrite]
OPTIONS:
--input-dir <input-dir> input dir path
--output-dir <output-dir>
output dir path for generated files
--overwrite overwrite files
-h, --help Show help information.
例子
如果您有如下代码,
struct Item {
struct SubItem {
var name: String
var description: String
public mutating func update(name: String) {
self.name = name
}
}
var name: String
var description: String
var subItems: [SubItem]
mutating func update(name: String) {
self.name = name
}
mutating func update(description: String) {
self.description = description
}
}
该工具能够生成以下代码
extension Item {
@_disfavoredOverload
func update(name: String) -> Self {
var new = self
new.update(name: name)
return new
}
@_disfavoredOverload
func update(description: String) -> Self {
var new = self
new.update(description: description)
return new
}
}
extension Item.SubItem {
@_disfavoredOverload
public
func update(name: String) -> Self {
var new = self
new.update(name: name)
return new
}
}
然后就可以连接调用方法如下
let item = Item(name: "Hello", description: "hello", subItems: [])
.update(name: "こんにちは")
.update(description: "こんにちは")
执照
swift-method-chainable 在 MIT 许可证下发布。见许可证