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

前端 未结 10 889
醉梦人生
醉梦人生 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...

    0 讨论(0)
  • 2020-11-27 10:26

    Although @MartinR's and @StephenChen's answer are perfect, Swift 4 changes things a little bit.

    Private is now considered as private to a class in which it is declared and also to its extensions.

    FilePrivate is considered to be private in that file be it a class in which the variable is defined, it's extension, or any other classes defined in that same file.

    0 讨论(0)
  • 2020-11-27 10:31

    A practical rule of thumb is that you use private for variables, constants, inner structs and classes that are used only inside the declaration of your class / struct. You use fileprivate for things that are used inside of your extensions within the same file as your class/struct but outside of their defining curly braces (ie. their lexical scope).

        class ViewController: UIViewController {
            @IBOutlet var tableView: UITableView!
            //This is not used outside of class Viewcontroller
            private var titleText = "Demo"
            //This gets used in the extension
            fileprivate var list = [String]()
            override func viewDidLoad() {
                navigationItem.title = titleText
            }
        }
    
        extension ViewController: UITableViewDataSource {
            func numberOfSections(in tableView: UITableView) -> Int {
                return list.count
            }
        }
    
    0 讨论(0)
  • 2020-11-27 10:32

    filePrivate - access controll level is within the file.

    case 1: If we create extension with in same class file and try to access fileprivate function or fileprivate property in its extension - access allowed
    case 2: If we create a extension of class in new file - And now try to access fileprivate function or fileprivate property - access not allowed

    private - access control level is with in lexical scope

    case 1: If property or function is declared as private in class - then scope is by default the class. case 2: if private instance is declared with in function body - then scope of instance is limited to function body.

    0 讨论(0)
提交回复
热议问题