Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
483 views
in Technique[技术] by (71.8m points)

swift - Is there a way to find actual SwiftUI API Documentation (and not just the developer documentation)?

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...