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!
OpenSwiftUI is an OpenSource implementation of Apple's SwiftUI DSL (Domain-specific language). The project's goal is to stay close to the original API as possible. Currently, this project is in early development.
Apple's online documentation is at https://developer.apple.com/documentation/, and although you're likely to have a local copy in Xcode most folks I speak to use the online version just because they can find things more easily.
SwiftUI is built in Swift, for Swift, and makes it easier to write and understand code. It is designed to help developers build better apps with far less code—potentially helping make Swift even more popular among developers than it already is.
A view that arranges its subviews in a vertical line.
SwiftUI provides views, controls, and layout structures for declaring your app’s user interface. The framework provides event handlers for delivering taps, gestures, and other types of input to your app, and tools to manage the flow of data from your app’s models down to the views and controls that users will see and interact with.
SwiftUI has made building user interface straightforward. API stands for Application Programming Interface. At some point, you would have built or will build APIs for your app. API allows your application to communicate to the server. One most important point to note is, API is just an interface between your application and the server/database.
Apple’s SwiftUI documentation covers a lot of the basics of SwiftUI. But what about those gaps that still exist? We all know that the documentation is likely to be updated massively when WWDC comes in June 2020, but we can’t wait that long! Here is everything I’ve learned about eve r y page of SwiftUI’s existing documentation.
Without marking a variable as State (within the struct) or Published (in an outside class conforming to ObservableObject ), SwiftUI will not redraw the contents of the View when the value changes.
What you've shown from that presentation isn't an implementation detail. It is the public init
method of VStack
. Note that it doesn't have a method body—it is not the implementation of the init
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 that init
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:
// Copyright © 2015 Apple Inc. All rights reserved.
#import <AppKit/AppKit.h>
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
:
/// A view that arranges its children in a vertical line.
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public struct VStack<Content> where Content : View {
/// Creates an instance with the given `spacing` and Y axis `alignment`.
///
/// - Parameters:
/// - alignment: the guide that will have the same horizontal screen
/// coordinate for all children.
/// - spacing: the distance between adjacent children, or nil if the
/// stack should choose a default distance for each pair of children.
@inlinable public init(alignment: HorizontalAlignment = .center, spacing: Length? = nil, content: () -> Content)
/// The type of view representing the body of this view.
///
/// When you create a custom view, Swift infers this type from your
/// implementation of the required `body` property.
public typealias Body = Never
}
Unfortunately, this (synthesized) declaration omits the @ViewBuilder
attribute on the content
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 of VStack
also doesn't mention that VStack
conforms to View
. (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 to View
is because VStack
doesn't conform directly to View
. VStack
conforms to _UnaryView
, and _UnaryView
is a subprotocol of View
.
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 your Xcode.app
:
Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks/SwiftUI.framework/Modules/SwiftUI.swiftmodule/arm64.swiftinterface
(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 of VStack
:
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
@_fixed_layout public struct VStack<Content> : _UnaryView where Content : SwiftUI.View {
@usableFromInline
internal var _tree: _VariadicView.Tree<_VStackLayout, Content>
@inlinable public init(alignment: HorizontalAlignment = .center, spacing: Length? = nil, @SwiftUI.ViewBuilder content: () -> Content) {
_tree = .init(
root: _VStackLayout(alignment: alignment, spacing: spacing), content: content())
}
public static func _makeView(view: _GraphValue<VStack>, inputs: _ViewInputs) -> _ViewOutputs
public static func _makeViewList(view: _GraphValue<VStack>, inputs: _ViewListInputs) -> _ViewListOutputs
public typealias Body = Swift.Never
}
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 of Color
, you need to search for both struct Color
and extension Color
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With