Is it possible to access a struct from another class?
ex:
class A{
struct structOfClassA {
func returnLetterA () -> String{
return "a"
}
}
}
class B{
let classA = A()
init(){
classA.structOfClassA.returnLetterA // this is what I want to achieve
}
}
how can I access the the struct from Class A() in Class B()?
is there a workaround with this?
Thank you!
You have just declared the struct A in class A but you also have to create an instance from struct A.
The structure in class
A
defines a type (that can be used within the scope of classA
), but you need an instance of it to be able to call the member functions of the structure. E.g.:Alternatively, you can let
B
be a subclass ofA
, which gives you access to the typeStructOfClassA
from the superclass, in which case you could create an instance ofStructOfClassA
and access its methodreturnLetterA()
: