2

Recently Swift introduced the access modifiers. What about setting some stuff in my AppDelegate private? I wanted some of the ivars be private, like window, for example. But I can't do so, because compiler shows a warning that I'm "Declaring a public var for an internal class" and recommends a window property to be internal. Well, OK. I do so. But later on I still can access that variable and if I set:

let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
appDelegate.window = nil // <- Accessible

the app crashes of course. Is there a way to make the real protection for that kind of stuff?

A gee
  • 21
  • 1

1 Answers1

1

I hate to be the guy who just posts a link and tells you to read it, so I will do my best to summarize, but read this article if you really want a great detailed explanation and walkthrough of the Access Controls in swift.

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AccessControl.html

From that doc: "All entities in your code (with a few specific exceptions, as described later in this chapter) have a default access level of internal if you do not specify an explicit access level yourself. As a result, in many cases you do not need to specify an explicit access level in your code"

So, unless you have some less-then-common use case, it seems to me that the appdelegate's window property is fine with the access control variable it is born with.

Michael Voznesensky
  • 1,612
  • 12
  • 15
  • 1
    What I was actually talking about is that compiler doesn't want me to set any of AppDelegate ivars to `private`. The warning appears as stated in the original post. And THAT is a problem. – A gee Oct 11 '14 at 18:42