Sunday, October 8, 2017

Swift 4 NEW Tutorial-3 (private instead of fileprivate)

In Swift 3, To access a private property in the extension in the same file, We need to specify it as fileprivate.

------------------------------------------------------------------------------------

class Vehicle {
    fileprivate var name = "BMW"
}

extension Vehicle {
    func strat() {
        print("Stating \(name)")
    }
    
    func stop() {
        print("Stopping \(name)")
    }
}

------------------------------------------------------------------------------------

Swift 4 solved this and we can access all the private properties in all the extensions of the class with in the same file.

------------------------------------------------------------------------------------

class Vehicle {
    private var name = "BMW"
}

extension Vehicle {
    func strat() {
        print("Stating \(name)")
    }
    
    func stop() {
        print("Stopping \(name)")
    }
}

------------------------------------------------------------------------------------


Hope this post is useful. Feel free to comment incase of any queries.


No comments:

Post a Comment