1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Swift】swift-depenciesの引数名を指定できない問題の対策

Last updated at Posted at 2023-08-22

はじめに

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: "引数")

おわり

いい感じになりました

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?