Swift how to make public variable in NSObject - ios

This is my class:
public class NewsListItem: NSObject {
var entries: [NewsListEntry]? = []
}
I can parse JSON string into this object using EVReflection:
let newsListItem = NewsListItem(json: responseObject)
But how do i make entries public? I Can't access newsListItem.entries

You can declare and access class variable in a NSObject class like below.
public static var entries = ""
and can acess from class name
print(NewsListItem.entries)

Related

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

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

Make class type a Dictionary key (Equatable, Hashable)

Say I have a class named LivingCreature
And other classes that inherit from it:
Human
Dog
Alien
This is what I'm trying to accomplish:
let valueForLivingCreature = Dictionary<Alien, String>
And access it like so:
let alienValue = livingCreatureForValue[Alien]
But this means the class should conform to Equatable and Hashable, but the class itself, not the class instance.
I've tried various ways of accomplishing this, but no luck.
As a compromise I've came up with is:
typealias IndexingValue = Int
class LivingCreature {
static var indexingValue: IndexingValue = 0
}
And then I can use the class as a key like so:
let livingCreatureForValue = Dictionary<IndexingValue, String>
Access:
let alienValue = livingCreatureForValue[Alien.indexingValue]
But, this way the IndexingValue should be set per class, by hand.
I would like to make a hash from the class itself like so:
class LivingCreature {
static var indexingValue: IndexingValue {
return NSStringFromClass(self).hash
}
}
This is not possible because self is not accessible is static var.
My question is, is there a better way of addressing this kind of issue?
Edit:
#Paulw11 Asked me why not make LivingCreature confirm to Equatable and Hashable,
The reason is I would not be able to access the value by the class type reference.
I would have to alloc an instance every time:
let alienValue = livingCreatureForValue[Alien()]
I do not want to call "Alien()" every time for finding a value.
And the component that uses it, doesn't care about the livingCreature instance, only about the class type.
I assume your are trying something like:
let valueForLivingCreature = Dictionary<LivingCreature.Type, String>
and:
let alienValue = valueForLivingCreature[Alien.self]
Then you can use ObjectIdentifier:
class LivingCreature {
class var classIdentifier: ObjectIdentifier {
return ObjectIdentifier(self)
}
//...
}
class Human: LivingCreature {
//...
}
class Dog: LivingCreature {
//...
}
class Alien: LivingCreature {
//...
}
let valueForLivingCreature: Dictionary<ObjectIdentifier, String> = [
Human.classIdentifier: String(Human),
Dog.classIdentifier: String(Dog),
Alien.classIdentifier: String(Alien),
]
let alienValue = valueForLivingCreature[Alien.classIdentifier] //->"Alien"
But in most use cases when you want to use meta-class as a dictionary key, you can find another way around:
class LivingCreature {
class var classValue: String {
return String(self)
}
//...
}
class Human: LivingCreature {
//...
//Override `classValue` if needed.
}
class Dog: LivingCreature {
//...
}
class Alien: LivingCreature {
//...
}
let alienValue = Alien.classValue //->"Alien"

Singleton and Class Properties in Swift

I am trying to create a singleton class. For this I have tried to use two different approaches i.e.
1.First approach - Employee class contains two instance properties, a class property that contains the shared instance of the class and a private initializer, i.e.
class Employee
{
var firstName : String
var lastName : String
static let sharedInstance = Employee(firstName: "Payal", lastName: "Gupta")
private init(firstName : String, lastName : String)
{
self.firstName = firstName
self.lastName = lastName
}
}
2.Second approach - Employee2 class contains two class properties, i.e.
class Employee2
{
static var firstName : String = "SomeFirsrName"
static var lastName : String = "SomeLastName"
}
Are these two approaches of making the singleton equivalent? If yes, which one should I use and what are the differences between each of them in respect of singleton?
To make a simple singletone class in Swift you could write:
class SomeManager {
static let sharedInstance = SomeManager()
}
Usage:
SomeManager.sharedInstance
What does it mean?
Since Swift 1.2 it's possible do declare static class properties. So you can implement the singleton like this. There can only be one instance for the lifetime of the application it exists in. Singletons exist to give us singular global state.
The first approach create a singletone having a class with this initialization: Employee(firstName: "Payal", lastName: "Gupta")
The second approach don't create a singletone, is a simple class with two static declared properties.
These two approaches are not equivalent:
The first approach creates an instance of Employee object
The second approach defines two class fields; it does not create any object instances.
In other words, only the first approach creates a singleton. The second approach makes a collection of related fields at the class level.
Try this...
class Employee : NSObject
{
var firstName : String
var lastName : String
class var sharedInstance: Employee {
struct Static {
static let instance: Employee = Employee(firstName: "Payal", lastName: "Gupta")
}
return Static.instance
}
private init(firstName : String, lastName : String)
{
self.firstName = firstName
self.lastName = lastName
}
}

How to define array of closures in Swift?

I want to define like this:
public var reloadFRCsNeedToPerformWhenFail = [()->()]()
but I get an error
Like this:
public var reloadFRCsNeedToPerformWhenFail : [()->()] = []
If you use a type alias to make ()->() a type, you can do it your way:
public typealias VoidVoid = ()->()
public var reloadFRCsNeedToPerformWhenFail = [VoidVoid]()
Or, forego the [] shortcut notation and use the full generic:
public var reloadFRCsNeedToPerformWhenFail = Array<()->()>()

Resources