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

前端 未结 10 1118
醉梦人生
醉梦人生 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:25

    class Privacy {
    
        fileprivate(set) var pu:Int {
            get {
                return self.pr
            }
            set {
                self.pr = newValue
            }
        }
        private var pr:Int = 0
        fileprivate var fp:Int = 0
    
    
        func ex() {
            print("\(self.pu) == \(self.pr) and not \(self.fp)")
        }
    }
    
    
    extension Privacy {
    
        func ex2() {
            self.pu = 5
            self.ex()
        }
    
    }
    

    I like this because it is super simple for ivars.

    Try changing fileprivate to private (and vice versa) and see what happens on compile...

提交回复
热议问题