Private class member accessible outside class - ios

Why are we allowed to assign public access specifier for a member in a private class i.e. incorrectVariable in the code below:
My code doesn't give compilation error and run properly, my code is:
private class C {
public var incorrectVariable = "SomeString"
var a = 5
func fooFun() -> Int {
self.a += 1
return self.a
}
}
var obj = C().a
print(obj)
obj = C().fooFun()
print(obj)

If you're creating private class object with same file there is not an issue. Private class not accessible in other file.
Refer this access control for detail link

Related

Why private variable declared outside the class can be access Class on the same file?

While studying iOS stuffs, I encountered this private var declared outside the class on the same file that it can access the private var in any class. Private definition is any type that can be access only within the class itself, but not other class.
private let outside: String = "private-let?"
class LoginVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print(outside)
}
}
Declaring any variables outside scope of class or struct will make it a Global Variable.
private let outside: String = "private-let?"
class LoginVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print(outside)
}
}
So in your case even you create a private constant or variable globally it will be available inside all the class you defined in that file. So the access level will be considered to that file-level scope.
If you make it public it will be available in other file classes as well.
Read more about the variables here
Read more about the access control
Let say we have a file Helper.swift and since our private variable is outside the class which makes it Globally accessible.
You will not get any compile time error while accessing private member because your classes are in same file. But if two classes were in different file then you would get compile time error.
//Since this private variable is outside the class So its Global and accessible to any class within file.
private var secretCode : String? = nil
class MyPrivateData {
init() {
secretCode = "First change"
}
}
class MySecondClass {
init() {
secretCode = "Second change"
}
}
private is intended for use inside a type declaration. Otherwise it has no meaning. When private is used in a place where it has no meaning, it is reinterpreted as fileprivate. That is what happens here. This code is in the same file so the fileprivate variable is visible.

How to run code when a static instance is initialized?

I'm using this pattern:
open class CoreDataHub {
public static let sharedInstance = CoreDataHub()
open class func getThing() -> String {
return "hello world"
}
init() {
sharedInstance.getThing()
}
}
But I get the following error:
Static member 'getThing' cannot be used on instance of type 'CoreDataHub'
My goal is to run some code...namely call getThing() when the sharedInstance is initialized. Any ideas? Thank you
You are getting
Static member 'getThing' cannot be used on instance of type 'CoreDataHub'
because, you are called getThing() static method using sharedInstance object.
Use CoreDataHub.getThing() instead of sharedInstance.getThing()
public static let sharedInstance = CoreDataHub()
init() {
let value = CoreDataHub.getThing()
}

Get data from class with two array class Swift

I have a class with two array, i need this class to work with it in tableview so this is my code
class X {
private var abc: [Demo]!
private var def: [Project]!
init() {
}
init(abc:[Demo], def:[Project]) {
self.abc = abc
self.def = def
}
}
So how can i get access to class Demo and Class Project, i have already the data in class X
class Demo like this
class Demo {
private var nom:String
init(nom:String) {
self.nom = nom
}
and class Project like this
class Project {
private var title:String
init(title:String){
self.title = tile
}
You haven't posted the code where you're trying to access the data, but your problem may be that you have declared nom and title as private which makes them inaccessible to other classes. Try deleting private.
If you just want to prevent other classes from changing them, you can change private to public private(set).
what 's mean of "get access"?
If your property in class Demo and Project is private , so you must have a get function to access to property in it

Can we access overridden class function from the base class in swift?

Am stuck in a situation where I have a let variable declared & initialized in base class. I would need to pass a different enum in one of my other classes extending this base class.
So, I tried creating a class function in base class so that I can override and return a different enum type. But is there any way that I can access the extended class from base class ?
Created a sample code below to help explain:
class A {
var string: String {
get {
// Is it possible to refer to the class type dynamically here ?
// So that it would call B's printMessage
return A.printMessage("Hello")
}
}
class func printMessage(message: String) -> String {
return "You shall not pass !"
}
}
class B: A {
override class func printMessage(message:String) -> String {
return message + "World !"
}
}
let obj = B()
print(obj.string)
make it like this:
var string: String {
get {
// Is it possible to refer to the class type dynamically here ?
// So that it would call B's printMessage
return self.dynamicType.printMessage("Hello")
}
}

Private static variable is accessible in different classes?

I declared a private static variable in one class. In the other file I could see them in the auto-complete. Is it some kind of a bug?
class Apple {
private static var name = "Apple"
}
In different file,
class Steve {
func call() {
// I could autocomplete, but it is not accessible as it says no member with the name
let name = Apple.name
}
}
After compile you can see auto-complete.

Resources