我目前正在观看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
)
}
Run Code Online (Sandbox Code Playgroud)
实际提供的文档代码:
struct VStack<Content> where Content : View
Run Code Online (Sandbox Code Playgroud)
上面关于对齐默认值的细节以及单独的间距长度虽然非常简单,但已经作为跨站点的单独SO问题被问及关于默认行为是什么以及为什么是默认行为的问题。
我想我要问的是,例如,对于UIKit中当前的,更完善的类,是否有办法查看类本身的实际实现细节,例如在此SwiftDC VStack的WWDC中显示的?我不确定这是苹果公司从未提供过的信息,还是人们随着时间的推移而学到的信息(这种行为以某种方式“只是因为”并且我们从经验中知道),或者这仅仅是事实, SwiftUI是新功能,Apple还没来得及巩固SwiftUI和文档。
抱歉,是否有人问过这个问题,或者这是一个非常明显的问题,总体而言,我还是一个新手。
谢谢!
您从该演示文稿中显示的内容不是实施细节。它是的公开init
方法VStack
。请注意,它没有方法主体-它不是init
方法的实现,只有它的类型签名。
你可以找到链接的信息相同的VStack
文档 “创建堆栈”的目标之下。这是该init
方法的文档页面。
您还可以在Xcode中看到这样的方法签名以及文档注释(如果有)。
在Xcode 11中,从菜单栏中选择“文件”>“快速打开...”(默认快捷方式:)??O。在快速打开栏中键入“ swiftui”:
打开“ SwiftUI.h”。它并没有说太多:
// Copyright © 2015 Apple Inc. All rights reserved.
#import <AppKit/AppKit.h>
Run Code Online (Sandbox Code Playgroud)
现在,单击编辑器左上角的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
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这个(综合的)声明忽略@ViewBuilder
了content
参数上的属性。这种遗漏可能是一个错误。
除了省略注释之外,Swift的生成接口还省略了以开头的类型,方法和属性_
。(忽略这些是因为它们被视为实现细节,无论出于何种原因都必须将它们公开。)例如,请注意,生成的接口VStack
也未提及VStack
符合的情况View
。(参考文档也没有提到它。)
生成的接口和参考文档都不告诉我们VStack
符合View
的原因是因为VStack
不直接符合View
。VStack
符合_UnaryView
,_UnaryView
是的子协议View
。
VStack
通过跟踪.swiftinterface
模块文件,您可以看到真实的,诚实的公用接口(在源代码导入SwiftUI时编译器实际使用的接口)。您可以在以下路径找到它,相对于您的Xcode.app
:
内容/开发人员/平台/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks/SwiftUI.framework/Modules/SwiftUI.swiftmodule/arm64.swiftinterface
(因此,/Applications/Xcode-beta.app/
如果您尚未重命名Xcode 11 beta并将其放入,请放在该路径的前面/Applications
)。
如果在该文件中搜索,struct VStack
则会找到的真正公共接口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
}
Run Code Online (Sandbox Code Playgroud)
请注意,该.swiftinterface
文件不会合并扩展名。VStack
没有任何扩展名,但是(例如)有扩展名,Color
因此,如果您想查看的真实公共接口Color
,则需要搜索struct Color
和extension Color
。
归档时间: |
|
查看次数: |
829 次 |
最近记录: |