9

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!

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
bemental
  • 103
  • 2
  • 7

2 Answers2

23

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 quickly bar with swiftui

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:

SwiftUI under Related Items 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:

finding vstack in the jump menu

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.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • 1
    This is exactly the answer I'm looking for, thank you so much Rob for the time it took to answer my question. I think this finally puts to bed why I still don't quite understand documentation, since some of this is pretty... *out of the way*, to say the least. Thank you again for taking the time to help (maybe you drop STS a DM ;) (https://twitter.com/stroughtonsmith))! – bemental Jun 10 '19 at 23:36
  • 1
    [Steven Troughton-Smith](https://stackoverflow.com/users/1851099/steven-troughton-smith) is probably already better at this sort of spelunking than I am. – rob mayoff Jun 11 '19 at 00:39
  • As if! On a side note, any chance you have a suggestion for finding a list of all the new vector SystemImages that are available? The Handling User Input tutorial only mentions one `Image(systemName: "star.fill")` and since they're accessed by a string I'm not able to .dotNotation research. Thanks again! – bemental Jun 11 '19 at 10:22
  • That deserves to be posted as a separate question. – rob mayoff Jun 11 '19 at 15:55
  • Rob, while not an answer to the new list of system image strings, thought you might find this Apple resource interesting: https://developer.apple.com/design/resources/ – bemental Jun 12 '19 at 20:26
  • and.... here's all of the symbols - Apple put them in an app for downloading on MacOS for browsing. https://developer.apple.com/design/human-interface-guidelines/sf-symbols/overview/ – bemental Jun 15 '19 at 00:58
2

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.

Krames
  • 364
  • 2
  • 4
  • Appreciate the response, updated my original question with a bit more specifics regarding the actual documentation code provided from Apple. Even looking at other, more established classes such as a UIViewController, the documentation seems to be lacking the specifics of what I was trying to get at in my question (`class UIViewController : UIResponder`). It’s one of my primary frustrations with reading documentation as a young programmer. – bemental Jun 10 '19 at 16:16