【发布时间】:2021-10-07 00:55:32
【问题描述】:
我在 SwiftUI 应用程序生命周期中工作。我刚刚更新了 Xcode,并在一个应用程序上遇到了一堆错误,该应用程序在稍旧版本的 Xcode 上运行良好...... '。
import Swift
import SwiftUI
#if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)
public enum AppKitOrUIKitViewControllerLifecycleEvent {
case didLoad
case willAppear
case didAppear
case willDisappear
case didDisappear
case layoutSubviews
}
struct _AppKitOrUIKitViewControllerLifecycleEventView<Content: View>: UIViewControllerRepresentable {
struct Callbacks {
var onDidLoad: ((UIViewController) -> Void)?
var onWillAppear: ((UIViewController) -> Void)?
var onDidAppear: ((UIViewController) -> Void)?
var onWillDisappear: ((UIViewController) -> Void)?
var onDidDisappear: ((UIViewController) -> Void)?
var onWillLayoutSubviews: ((UIViewController) -> Void)?
var onLayoutSubviews: ((UIViewController) -> Void)?
mutating func setCallback(
_ callback: ((UIViewController) -> Void)?,
for event: AppKitOrUIKitViewControllerLifecycleEvent
) {
switch event {
case .didLoad:
self.onDidLoad = callback
case .willAppear:
self.onWillAppear = callback
case .didAppear:
self.onDidAppear = callback
case .willDisappear:
self.onWillDisappear = callback
case .didDisappear:
self.onDidDisappear = callback
case .layoutSubviews:
self.onLayoutSubviews = callback
}
}
}
class UIViewControllerType: UIHostingController<Content> {
var callbacks: Callbacks?
override func viewDidLoad() {
super.viewDidLoad()
callbacks?.onDidLoad?(self)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
callbacks?.onWillAppear?(self)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
callbacks?.onDidAppear?(self)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
callbacks?.onWillDisappear?(self)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
callbacks?.onDidDisappear?(self)
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
callbacks?.onWillLayoutSubviews?(self)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
callbacks?.onLayoutSubviews?(self)
}
}
func makeUIViewController(context: Context) -> UIViewControllerType {
UIViewControllerType() //root error view here
}
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
}
}
#endif
在 List++.swift 中,我得到上下文闭包类型 '(type1,type2) -> RowContent' 需要 2 个参数,但闭包主体中使用了 1。
import Swift
import SwiftUI
extension List {
@available(watchOS, unavailable)
public init<Data: RandomAccessCollection, RowContent: View>(
_ data: Data,
selection: Binding<Set<SelectionValue>>,
@ViewBuilder rowContent: @escaping (Data.Element, _ isSelected: Bool) -> RowContent
) where Data.Element: Identifiable, Content == ForEach<Data, Data.Element.ID, HStack<RowContent>>, SelectionValue == Data.Element.ID {
self.init(data, selection: selection, rowContent: { element in //here this error appears
rowContent(element, selection.wrappedValue.contains(element.id))
})
}
@available(watchOS, unavailable)
public init<Data: RandomAccessCollection, RowContent: View>(
_ data: Data,
selection: Binding<Set<SelectionValue>>,
@ViewBuilder rowContent: @escaping (Data.Element, _ isSelected: Bool) -> RowContent
) where Data.Element: Identifiable, Content == ForEach<Data, Data.Element.ID, HStack<RowContent>>, SelectionValue == Data.Element {
self.init(data, selection: selection, rowContent: { element in //here the error also appears
rowContent(element, selection.wrappedValue.contains(element))
})
}
}
extension List where SelectionValue == Never {
@available(watchOS, unavailable)
public init<Data: MutableCollection & RandomAccessCollection, RowContent: View>(
_ data: Binding<Data>,
@ViewBuilder rowContent: @escaping (Binding<Data.Element>) -> RowContent
) where Data.Element: Identifiable, Content == ForEach<AnyRandomAccessCollection<_IdentifiableElementOffsetPair<Data.Element, Data.Index>>, Data.Element.ID, RowContent> {
self.init {
ForEach(data) { (element: Binding<Data.Element>) -> RowContent in
rowContent(element)
}
}
}
}
最重要的是,我收到“Command CompileSwift failed with a nonzero exit code”错误。而且这些文件都不是可编辑的……不知道该怎么做。非常感谢任何帮助。
【问题讨论】:
-
错误在哪几行?
-
@aheze 将 cmets 添加到有错误的行中,对此感到抱歉
-
不确定是否相关,但您的回调中有
onWillLayoutSubviews,但枚举中缺少相应的案例。哪个版本的 Xcode 是“稍旧的版本”? -
如果您无法使其在 Xcode13 中运行,您可以下载 Xcode12,并使用它来构建应用程序,同时修复 Xcode13 中的错误(假设您保留两个 Xcode 安装端旁边)。
-
不可编辑的文件的来源是什么,是你的吗?
标签: ios swift xcode swiftui xcode13