我目前正在观看 WWDC 2019 的 SwiftUI Essentials 视频,演示者提取了 VStack 的实际 API,其中(令人难以置信,至少对我而言)详细说明了这个特定结构的实际工作原理。
开发人员是否可以在某个地方找到有关 Apple 提供的特定功能的这种详细、深入的文档?
Matt Stevens 提供了一个很棒的 APIdiff (http://codeworkshop.net/objc-diff/sdkdiffs/) 网站,但它都链接回 Apple 的标准开发人员文档页面。
演示者提供的示例代码:
public struct VStack<Content : View> : View {
public init(
alignment: HorizontalAlignment = .center,
spacing: Length? = nil,
@ViewBuilder content: () -> Content
)
}
实际提供的文档代码:
struct VStack<Content> where Content : View
上面关于对齐默认值和间距长度的细节,虽然 super 简单,但已经作为单独的 SO 问题在整个网站上被问及默认行为是什么以及为什么是这样。
我想我要问的是,例如,对于 UIKit 中当前更成熟的类,有没有办法查看类本身的实际实现细节,例如在本 WWDC 中显示的 SwiftUI VStack?我不确定这是否是 Apple 从未公开过的信息,是人们随着时间的推移了解到的信息(这在某种程度上“只是因为”我们从经验中知道这一点),或者这是否只是事实SwiftUI 是新的,Apple 还没有时间巩固 SwiftUI 和文档。
很抱歉,如果有人问过这个问题,或者这是一个非常明显的问题,我对软件开发总体来说还是个新手。
谢谢!
最佳答案
您在该演示文稿中展示的不是实现细节。是公众init
VStack
的方法| .请注意,它没有方法体——它不是 init
的实现。方法,只有它的类型签名。
您可以找到从 the VStack
documentation 链接的相同信息。在“创建堆栈”目标下。这是 the documentation page for that init
method .
您还可以在 Xcode 中看到这样的方法签名,如果有的话,还有文档注释。
在 Xcode 11 中,从菜单栏中选择"file">“快速打开...”(默认快捷方式:⇧⌘O)。在“快速打开”栏中输入“swiftui”:
打开“SwiftUI.h”。它并没有说太多:
// Copyright © 2015 Apple Inc. All rights reserved.
#import <AppKit/AppKit.h>
现在单击编辑器左上角的 Related Items 图标,然后从菜单中选择 Generated Interface > SwiftUI:
然后,在编辑器顶部的跳转栏中点击“SwiftUI”右边的“No Selection”,在跳转菜单打开的情况下输入“vstack”:
Xcode跳转到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
}
不幸的是,这个(合成的)声明省略了 @ViewBuilder
content
上的属性争论。这个遗漏可能是一个错误。
除了省略注解之外,Swift 生成的接口(interface)还省略了以_
开头的类型、方法和属性。 . (它省略了这些,因为它们被认为是实现细节,无论出于何种原因都必须公开。)例如,请注意 VStack
的生成接口(interface)。也没有提到 VStack
符合 View
. (The reference documentation也没有提到。)
生成的界面和引用文档都没有告诉我们的原因VStack
符合 View
是因为VStack
不直接符合 View
. VStack
符合 _UnaryView
, 和 _UnaryView
是 View
的子协议(protocol).
可以看到VStack
的真实的、老实交代的公共(public)接口(interface)| —当您的源代码导入 SwiftUI 时,编译器实际使用的是什么—通过追踪 .swiftinterface
模块的文件。您可以在此路径中找到它,相对于您的 Xcode.app
:
Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks/SwiftUI.framework/Modules/SwiftUI.swiftmodule/arm64.swiftinterface
(因此,如果您尚未重命名 Xcode 11 beta 并将其放在 /Applications/Xcode-beta.app/
中,请将 /Applications
放在该路径的前面。
如果您在该文件中搜索 struct VStack
, 你会发现 VStack
的真正公共(public)接口(interface):
@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
}
请注意 .swiftinterface
文件不合并扩展名。 VStack
没有任何扩展名,但是(例如)Color
确实如此,所以如果你想看到 Color
的真正公共(public)接口(interface),您需要同时搜索 struct Color
和 extension Color
.
关于swift - 有没有办法找到实际的 SwiftUI API 文档(而不仅仅是开发人员文档)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56528833/