kawingkelvin kawingkelvin - 8 months ago 53
Swift Question

What is behavior of private access control for swift class?

I tried this with storyboard with Xcode 7 GM Seed:

import UIKit

public class C {
let _secret = arc4random_uniform(1000)

private func secret() -> String {
return "\(_secret) is a secret"

let c1 = C()

This compiled and gave me the "secret". So this upsets my understanding of access control for Swift class and object. Why is this happening?


In Swift private means accessible only within the same source file which is what you're doing. If the code in your question was contained in a file C.swift and you would try to access the secret method from another Swift file you would get a compile-time error.

You can read more about the different access modifiers in the official documentation.