skip to Main Content

When using Swift, is there a way of using Xcode or any other tool to find out which initializer was used to instantiate a class? Let me provide this example from SwiftUI:

List {
  ForEach(names.array, id: .self) {
    name in Text(name)
  }
}

To understand the mechanics of List and build more elaborate lists, I’d want to know how this list was instantiated. The documentation shows me that List has around ten initializers, and given that I’m not an expert in the complex types involved, it’s hard for me to find out which one was used.

2

Answers


  1. Fundamentally, no.

    There may be signatures left on the final object based on which initialisers were used, but they would all be implementation specific, and potentially local to whatever version of the compiler you were using in the moment.

    The List(…) initialiser makes exactly one promise, and that is that you get a List back.

    Trying to extract more information than that is a fool’s errand; the information is simply not (by necessity) there.

    Login or Signup to reply.
  2. If you don’t mind adding explicit init, Cmd-clicking on init will move you to the right header.

            List.init {
                ForEach.init(names.array, id: .self) {
                name in Text(name)
              }
            }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search