Get data from class with two array class Swift - ios

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

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.

Private class member accessible outside class

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

How to create Singleton in generic class using swift 4.2?

I have been trying to convert a singleton class to generic. Since swift doesn't support stored property in generic class Singleton could not be implemented. I tried all the possible solutions that I know but nothing works. Other Stackoverflow answers related to this topic doesn't help either. Can someone help me to get on the right path, please? Thanks in advance.
I have added the sample code below.
class SingleTonClass<T: Equatable & RawRepresentable>: NSObject where T.RawValue == String {
private var catlog: CatlogModelClass<T>?
private var catlogArray = [CatlogModelClass<T>]()
**// This stored property Now has to be changed to support Generic**
private static var shared : SingleTonClass = {
return SingleTonClass()
}()
// Accessor for Singleton
static func sharedInstance() -> SingleTonClass {
return shared
}
private override init () {}
}
If you want it to be non-singleton, you need to create a public constructor.
You should pass in the catalog and catalogArray into the constructor and initialize self.catalog and self.catalogArray. You then need to remove the static shared instance of the class, because your new setup would require you to create an instance of it.

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.

Class initialisation in Swift

When I create two classes like so:
class Example
{
}
class OtherExample
{
init() {
println("created")
}
}
var instance = Example()
var otherInstance = OtherExample()
Both seem to create usable instances, so I'm wondering what the difference is in Swift if you don't provide an init method, and yet you initialise as above?
I did think it probably called the superclass init automatically, however since both of these objects don't inherit from NSObject, they don't have super classes do they?!
Also is there a need to class super.init() in the otherExample?
You only need super.init() if your class inherits from another class.
class Example {
func sayHi() {
print("hi")
}
}
class OtherExample: Example {
override init() {
super.init()
print("created")
}
}
var instance = Example()
instance.sayHi()
// hi
var otherInstance = OtherExample()
otherInstance.sayHi()
// created
// hi
class example{
var example:Int = 0
}
class anotherExample{
init(example:Int){
self.example = example
}
var example:Int
}
example()
anotherExample(0)
You can have not initialized variables in a class with an initializer, if you don’t have any initializer you will need to set a value in the class
Both will be able to change their example value but only anotherExample will have a value that can be set.
In my test playground I was unable to use super.init() in the classes (since they don’t have any superclasses to init to/from)

Resources