The structure in class A
defines a type (that can be used within the scope of class A
), but you need an instance of it to be able to call the member functions of the structure. E.g.:
class A {
struct StructOfClassA {
func returnLetterA() -> String{
return "a"
}
}
var structOfClassA = StructOfClassA()
/* Instance of 'StructOfClassA' structure type */
}
class B {
let classA = A()
init() {
let myLetter = classA.structOfClassA.returnLetterA()
print(myLetter)
}
}
var myB = B() // prints "a"
Alternatively, you can let B
be a subclass of A
, which gives you access to the type StructOfClassA
from the superclass, in which case you could create an instance of StructOfClassA
and access its method returnLetterA()
:
class A {
class StructOfClassA {
func returnLetterA() -> String{
return "a"
}
}
}
class B : A {
let classA = A()
override init() {
let myLetter = StructOfClassA().returnLetterA()
print(myLetter)
}
}
var myB = B() // prints "a"