0

Lets say we are extending a parent class A as

extension A {
    struct S {
        var x = ""
    }
}

and we have another class that inherits class A e.g.

class B: A {

}

As struct/enum are static members we can only call the struct S using class name A as:

A.S().x

I want to know if somehow it can be possible through some function we can call the variables inside struct using instance of class B. So e.g.

var c = B()

and i need the struct to be accessible via instance of class B like:

c.S().x

So can we write some function in class A which can return all the contents of struct S ? something like :

extension A {
    struct S {
        var x = ""
    }
    func getS() {
        // return all members of S (not just x, there can be multiple variables)
    }
}

so in the end I should be able to access S using: c.getS().x

1
  • S is not a static member, is an inner class, which it tied to the concrete type A. Commented Feb 24, 2018 at 17:37

2 Answers 2

3

"As struct/enum are static methods." I think this is where the confusion starts. structs are not methods. They're types. You don't "call" a struct. S() is just shorthand for S.init(), which returns a new instance. There is no "S" inside of an instance of A or B, because you've not defined any property of that type.

I believe the type you have in mind is really this:

class A {}

extension A {
    struct S {
        var x = ""
    }

    static var s = S()
}

This adds a static property s to A (and thus to B as well). Given that, you would access it as A.s.x or B.s.x.

If you have an instance of A or B and wanted to access it, that would be done through type(of:):

var b = B()
type(of: b).s.x

Your question about accessing the properties is separate from this. Maybe it's really the whole question, but in that case all the rest seems irrelevant. If you mean this:

struct S {
    var x = ""
    func getProperties() -> ??? {
        ???
    }
}

Then no, there's no way to create that in Swift. Swift has very limited introspection. It's not clear what you expect the return value to be even if it were possible, or what you could do with the results.

Sign up to request clarification or add additional context in comments.

Comments

0

return all members of S

No, you're asking for some sort of omniscient introspection that Swift doesn't have. If you want that sort of metaprogramming capability, use another programming language.

However, your overall question makes no sense on its face. You see to think that the declaration struct S means something special in relation to B. It doesn't. struct S is not an S instance; it's merely a type declaration, no more. It's a type declaration that you've namespaced; apart from that, it's no different than if you said struct S at the top level of any file, totally outside A or B.

And this premise:

As struct/enum are static methods

...is just blather; they are no such thing. They are not static and they are not methods. They are types (or, as in your use of the phrase struct S, the first word of type declarations).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.