Why doesn’t Swift call my overloaded method with a more specific type?

后端 未结 5 447
有刺的猬
有刺的猬 2021-01-02 15:38

I use Decodable to decode a simple struct from JSON. This works by conforming to a Decodable protocol:

extension BackendServerID: Decodable {

          


        
5条回答
  •  情深已故
    2021-01-02 16:15

    Swift is supposed to call the most specific implementation, you can try that in a playground to confirm; so your expectation is correct.

    In your case, I suspect that the issue lies in access control levels.

    In this Decodable library, method func decode(_ json: Any) is declared as public, thus it's available in your tests code.

    On the other hand, your own method func decode(_ string: String) doesn't seem to be public, and then is internal by default, and not accessible in your tests code.

    To fix that, either import your app's framework using @testable (which makes all internal symbols available), or declare the method public.

提交回复
热议问题