はじめに
swift-depenciesを使っていて、引数名がないので何を渡すのかがわからなくなることがよくありました。
問題
public struct SampleRepository {
public var sample: @Sendable (String) -> String
init(
sample: @Sendable @escaping (String) -> String
) {
self.sample = sample
}
}
使用する時
sample("引数")
解決策
import Foundation
public struct SampleRepository {
private var _sample: @Sendable (String) -> String
init(
_sample: @Sendable @escaping (String) -> String
) {
self._sample = _sample
}
}
extension SampleRepository {
public func sample(arg: String) -> String {
_sample(arg)
}
}
使用する時
sample(arg: "引数")
おわり
いい感じになりました