What is a good example to differentiate between fileprivate and private in Swift3

前端 未结 10 1089
醉梦人生
醉梦人生 2020-11-27 10:03

This article has been helpful in understanding the new access specifiers in Swift 3. It also gives some examples of different usages of fileprivate

10条回答
  •  情歌与酒
    2020-11-27 10:06

    In the following example, language constructs modified by private and fileprivate seem to behave identically:

    fileprivate func fact(_ n: Int) -> Int {
        if (n == 0) {
            return 1
        } else {
            return n * fact(n - 1)
        }
    }
    
    private func gauss(_ n: Int) -> Int {
        if (n == 0) {
            return 0
        } else {
            return n + gauss(n - 1)
        }
    }
    
    print(fact(0))
    print(fact(5))
    print(fact(3))
    
    print(gauss(10))
    print(gauss(9))
    

    This is according to intuition, I guess. But, is there any exception?

    Kindest regards.

提交回复
热议问题