Reputation: 1801
I am coming from java/C++/C# to Swift. I have the following issue:
class A {
struct Fields{
static let valA = "A";
}
}
class B: A {
struct Fields{
static let valB = "B";
static let valD = "D";
}
}
var b: B = B()
println("\(B.Fields.valB)")
The compiler is complaining that "Class A.Fields.Type has no member named "valB".
I need to keep the same enum name (eq. Fields) on both classes.
Thank you,
Upvotes: 1
Views: 1526
Reputation: 10058
instead of struct, make it class
class A {
class Fields{
static let valA = "A";
}
}
class B: A { //<--Doesn't have to extend A
class Fields : A.Fields{
static let valB = "B";
static let valD = "D";
}
}
println(B.Fields.valA)//A
println(B.Fields.valB)//B
println(B.Fields.valD)//D
edit: why class and not struct? struct can't be inherited
struct A{
var a : Int?;
}
struct B :A{
var b : Int;
}
this will generate compile error
Inheritance from non-protocol type 'A'
only classes can be inherited and only classes can extend other classes. this code will also generate compile error:
class A{
var e : Int?;
}
struct B :A{
var t : Int;
}
Non-class type 'B' cannot inherit from class 'A'
dont forget that structs comes from C language, which is not object oriented.
Upvotes: 1