I’m currently watching the SwiftUI Essentials video from WWDC 2019 and the presenter pulled up the actual API for a VStack, which (incredibly, at least to me) details how this particular struct actually works.
Is there a place for developers to find this sort of detailed, in-depth documentation about particular features provided by Apple?
Matt Stevens has an awesome APIdiff (http://codeworkshop.net/objc-diff/sdkdiffs/) site that he provides, but it all links back to Apple’s standard developer documentation pages.
Sample code provided by the presenter:
public struct VStack<Content : View> : View {
public init(
alignment: HorizontalAlignment = .center,
spacing: Length? = nil,
@ViewBuilder content: () -> Content
)
}
Actual provided documentation code:
struct VStack<Content> where Content : View
The specifics above regarding the alignment defaults, and the spacing length alone, while super simple, are already being asked as separate SO questions across the site in regards to what and why default behaviors are what they are.
I guess what I’m asking, is for current, more well-established classes in UIKit for example, is there a way to see the actual implementation details of the class themselves such as displayed in this WWDC for a SwiftUI VStack? I’m not sure if this is information that Apple just never gives out and is something that people have learned over time (this acts in a certain way “just because” and we know that from experience) or if this is just the fact that SwiftUI is new and hasn’t had time for Apple solidify SwiftUI and the documentation yet.
Sorry if this has been asked, or if it’s a super obvious question, I’m still pretty new to software development overall.
Thanks!
Right now the documentation on SwiftUI is pretty sparse. But I imagine it will continue to grow and improve from official and auxiliary sources.
Here are the main resources I am going off of right now:
You can also view the docs from within Xcode 11.0 beta
shift+command 0
and look under the SwiftUI dropdown.What you've shown from that presentation isn't an implementation detail. It is the public
init
method ofVStack
. Note that it doesn't have a method body—it is not the implementation of theinit
method, only its type signature.You can find the same information linked from the
VStack
documentation under the “Creating a Stack” target. Here's the documentation page for thatinit
method.You can also see method signatures like this, with doc comments if there are any, in Xcode.
In Xcode 11, choose File > Open Quickly… from the menu bar (default shortcut: ⇧⌘O). Type “swiftui” in the Open Quickly bar:
Open “SwiftUI.h”. It doesn't say much:
Now click the Related Items icon in the top left corner of the editor and choose Generated Interface > SwiftUI from the menu:
Then, click on “No Selection” to the right of “SwiftUI” in the jump bar at the top of the editor and type “vstack” while the jump menu is open:
Xcode jumps to the definition of
struct VStack
:Unfortunately, this (synthesized) declaration omits the
@ViewBuilder
attribute on thecontent
argument. This omission is probably a bug.In addition to omitting annotations, Swift's generated interface also omits types, methods, and properties that start with
_
. (It omits these because they are considered implementation details that for whatever reason have to be made public.) For example, notice that the generated interface ofVStack
also doesn't mention thatVStack
conforms toView
. (The reference documentation also doesn't mention it.)The reason that both the generated interface and the reference documentation don't tell us
VStack
conforms toView
is becauseVStack
doesn't conform directly toView
.VStack
conforms to_UnaryView
, and_UnaryView
is a subprotocol ofView
.You can see the real, honest-to-dog public interface of
VStack
—what the compiler actually uses when your source code imports SwiftUI—by tracking down the.swiftinterface
file for the module. You can find it at this path, relative to yourXcode.app
:(So, put
/Applications/Xcode-beta.app/
on the front of that path if you haven't renamed the Xcode 11 beta and have it in/Applications
).If you search that file for
struct VStack
, you'll find the true public interface ofVStack
:Note that the
.swiftinterface
file does not consolidate extensions.VStack
doesn't have any extensions, but (for example)Color
does, so if you want to see the true public interface ofColor
, you need to search for bothstruct Color
andextension Color
.