Swift. Declaring private functions in internal protocol

ⅰ亾dé卋堺 提交于 2019-12-19 09:18:34

问题


How can I achieve something like this (doesn't compile):

internal protocol InternalPrivateMix {
    private func doPrivately()
    internal func doInternaly()
}

Basically I want to kind of make a promise that confirming class implements some functionality privately. This is more for self documentation. I obviously can just implement these functions in my classes without formally conform to protocol and write documentation describing that every class should implement this functionality. Though it would be nice if I could communicate my intent to other developers more formally.

EDIT: I have tried to implement two protocols in one file, one private, one internal as @creeperspeak suggested. However I cannot conform to private protocol in other files so it doesn't work.


回答1:


From Apple's docs it looks like the only way to achieve what you are trying to do is to implement 2 protocols - one internal, and one private, as Apple states "You cannot set a protocol requirement to a different access level than the protocol it supports."




回答2:


You can do this:

protocol P {
    func int()
}

extension P {
    func int() {
        print("int()")
        priv()
    }
    private func priv() {
        print("priv()")
    }
}

Which might serve your purpose - I use it.



来源:https://stackoverflow.com/questions/42585775/swift-declaring-private-functions-in-internal-protocol

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!