Should not be needed create an instance of a class to access a public constant. I recently started working in Swift, so I must be missing something here.
In this simple example:
public class MyConstants{
public let constX=1;
}
public class Consumer{
func foo(){
var x = MyConstants.constX;// Compiler error: MyConstants don't have constX
}
}
This foo code gives an compile error. To work, I need to create an instance of the MyConstants like this:
public class Consumer{
func foo(){
var dummy = MyConstants();
var x = dummy.constX;
}
}
Adding static to constX is not allowed.
Use struct
with static
types.struct
are more appropriate as in enum
you can only bind one type of associative values but you can contain the "Type Property of any type" in both.
public struct MyConstants{
static let constX=1;
}
public class Consumer{
func foo(){
var x = MyConstants.constX;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With