My preview was working great. Then, without any code modifications, I went to show it to my colleagues and it started failing when I hit resume (I made no modifications to this project. I did work on other projects in Xcode in the meantime). Now I cannot make it work.
I've cleaned (Product | Clean Build Folder), and deleted ~/Library/Developer/Xcode/DerivedData.
When I hit Resume in the preview canvas, the top line of the canvas shows "Failed to build ViewControllerPreviews"
Clicking the Diagnostics button shows:
use of undeclared type 'ViewControllerPreviews'
----------------------------------------
.../Library/Developer/Xcode/DerivedData/ACUIKit-gjaonztkyzvcudalxmalekjsfacd/Build/Intermediates.noindex/Previews/ACUIKitApp/Intermediates.noindex/ACUIKit.build/Debug-iphonesimulator/ACUIKit.build/Objects-normal/x86_64/ViewControllerPreviews.2.preview-thunk.swift:11:11: error: use of undeclared type 'ViewControllerPreviews'
extension ViewControllerPreviews {
^~~~~~~~~~~~~~~~~~~~~~
ViewControllerPreviews is in the ACUIKitApp target. It is not the ACUIKit framework target.
ViewControllerPreviews is previewing UIViewControllers that are defined in the app (not the framework).
Inspecting the file referred to above, I see
@_private(sourceFile: "ViewControllerPreviews.swift")import ACUIKit
#if DEBUG
import ACUIKit
#endif
#if DEBUG
import SwiftUI
#endif
import SwiftUI
#if DEBUG
extension ViewControllerPreviews {
@_dynamicReplacement(for: updateUIViewController(_:context:)) private func __preview__updateUIViewController(_ uiView: ViewControllerPreviews.UIViewControllerType, context: UIViewControllerRepresentableContext) {
#sourceLocation(file: "/Users/jthomas/Dev/iOS-ACUIKit/ACUIKitApp/ViewControllerPreviews.swift", line: 94)
#sourceLocation()
}
}
#endif
. . .
There is a similar error message for every reference to ViewControllerPreviews in the file.
I've reduced my preview code to the following, and get the same error.
#if DEBUG
import SwiftUI
import ACUIKit
struct ViewControllerPreviews: PreviewProvider {
static var previews: some View {
ViewControllerPreviews()
}
}
extension ViewControllerPreviews: UIViewControllerRepresentable {
typealias UIViewControllerType = DynamicFontsTableViewController
func makeUIViewController(context: UIViewControllerRepresentableContext<ViewControllerPreviews>) -> DynamicFontsTableViewController {
DynamicFontsTableViewController(core: Core(state: AppState(theme: Theme.trees)))
}
func updateUIViewController(_ uiViewController: DynamicFontsTableViewController, context: UIViewControllerRepresentableContext<ViewControllerPreviews>) {
}
}
#endif
This happened to me before. Just deleting the file, then adding it back wasn't enough to fix the problem. The way I finally fixed it was to remove my project (delete project directory and re-clone from git), then add all the preview code back in again. I want to use previews, but I can't if they just quit working. My work-around is too much.
Xcode 11.3.1
Any idea what I can do to get this working again?