Friday, May 6, 2016

Private - Scope

I just discovered this and thought I would share it. Look at this code:
class Vehicle {
    private var color: String = "red"
}

var car = Vehicle()

print(car.color)

How am I able to access the color property even though it is private?


It turns out it is file based. If you are in the same file as the class you can access the private variables. But if you put your class in one file and then instantiate it another file then you cannot access the private variables.

(Swift 2.2)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

SwiftUI Search & Filter with Combine - Part 3 (iOS, Xcode 13, SwiftUI, 2...

In part 3 of the Searchable video series, I show you how to use Combine in #SwiftUI for the search and filter logic connected to the searcha...