Implement an open protocol with an inner class in swift

So, I am writing a Swift structure where I want to publish some objects publicly only through protocol.

eg:

public protocol Thing : class {
    var x : Int { get }
}

I want to hide the details of my implementation, so I create a class internal:

internal class ThingImpl : Thing {
    public private(set) var x : Int = 0
}

And release a new one Thingsusing the factory method:

public func newThing() -> Thing {
    return ThingImpl()
}

I can’t understand why the compiler warns me about creating var x publicinside ThingImpland calls me to declare it internal.

enter image description here

Everything works fine, despite the warning, and if I listen to the warning and go on internal private(set) var x : Int = 0, the warning will disappear and everything will work fine.

, var , , . - ? .

+4
2

ThingImpl - . , x, . , public .

+2

this :

  • private:
  • :
  • public:

:

internal class ThingImpl : Thing {
    <#access#> var x : Int = 0
}

:

  • private: , x Thing ( ).
  • public: , ( )
  • private(set): x
  • internal: , - , x (framework, target ..).
  • public private(set): public private , ?. , internal.

:

  • x
  • private(set),
0

Source: https://habr.com/ru/post/1613826/


All Articles