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<()->()>()
Related
I have seen some tutorials which declare mutableState in this way:
class SomeViewModel {
private val _loadingMap = mutableStateOf(false)
val loadingMap = _loadingMap
}
What would be the advantage about it?
I'm using my mutableStates just like this:
class SomeViewModel {
val loadingMap = mutableStateOf(false)
}
should I use the first code structure? or is a overkill implementation
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
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"
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)
Is it possible? The error Only syntatic function types can be generic suggests it isn't.
Valid Code
func test<T:Equatable>(function: (T) -> T){
var myArray:Array<T -> T> = [function];
}
Now I want to make a property with the same type as myArray. I feel like I should be able to do this somehow.
Doesn't work
var myArray:<T:Equatable>(T -> T)[]
Any ideas?
Even this:
func test<T:Equatable>(function: (T) -> T){
var myArray:Array<T -> T> = function;
}
Shouldn't be valid. You are assigning a T->T to a Array<T->T>. It should be at least:
var myArray:Array<T -> T> = [function];
This would work:
class MyClass<T> {
var myArray:(T->T)[] = []
}
Since you now have a generic MyClass class, to initialise, you need to tell what type T is, like so:
var x = MyClass<String>()