I'm currently using a custom NavigationStack
to control my navigations, as proposed in this article.
I am able to run Build, Simulate, Test, and Analyze successfully, but when running Archive, Xcode throws an error: Segmentation fault: 11
, right before completion.
I've pinpointed the code causing this error to a spot within the advance(_:)
method (in NavigationStack.swift
) through trial and error.
func advance<V: View & Navigatable>(_ view: V) {
let item = NavigationItem(view)
viewStack.append( currentView )
withAnimation {
self.navigate = .forward
}
//>>>>>>>> HERE'S THE PROBLEM <<<<<<<<<
// ↓this↓ causes the Archive to fail.
currentView = item
}
My Environment: macOS Catalina - Version 10.15.2, Xcode - Version 11.3 (11C29). (However, I had this issue before updating from macOSCatalina10.15.1 and Xcode11.2)
If it helps, my other source codes are as follows:
SceneDelegate.swift:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
...
let contentView = ContentView()//.environmentObject(UserData())
...
}
ContentView.swift:
struct ContentView: View {
//@EnvironmentObject var userData: UserData
var body: some View {
NavigationHost()
//.environmentObject(NavigationStack( --Commented out in Edit
// NavigationItem(view: AnyView( HomeView() ) --Commented out in Edit
.environmentObject(NavigationStack( HomeView() ))
.transition(.move(edge: .trailing))
//.environmentObject(userData)
}
}
NavigationHost.swift:
struct NavigationHost: View{
@EnvironmentObject var navigation: NavigationStack
//@EnvironmentObject var userData: UserData
var body: some View {
ZStack {
if self.navigation.navigate == .root {
self.navigation.currentView.view
//.environmentObject(self.userData)
.environmentObject(self.navigation)
}
else if self.navigation.navigate == .backward {
self.navigation.currentView.view
//.environmentObject(self.userData)
.environmentObject(self.navigation)
.transition(.move(edge: .leading))
}
if self.navigation.navigate == .forward {
self.navigation.currentView.view
//.environmentObject(self.userData)
.environmentObject(self.navigation)
.transition(.move(edge: .trailing))
}
...
}
}
}
NavigationStack.swift:
final class NavigationStack: ObservableObject {
enum Direction {
case root
case forward
case backward
}
@Published var viewStack: [NavigationItem] = []
@Published var currentView: NavigationItem
@Published var navigate: Direction = .root
@Published var turnOnCloseButton: Bool = false
@Published var turnOnBackButton: Bool = false
init<V: View & Navigatable>(_ view: V, description: String? = nil) {
let currentView = NavigationItem(view, description: description)
print("Navigation Stack Initialized")
self.currentView = currentView
}
func back(turnOffBack: Bool = true, turnOffClose: Bool = true) {
if turnOffBack { turnOnBackButton = false }
if turnOffClose { turnOnCloseButton = false }
if viewStack.count == 0 {
return
}
let last = viewStack.count - 1
currentView = viewStack[last]
withAnimation {
self.navigate = .backward
}
viewStack.remove(at: last)
}
func advance<V: View & Navigatable>(_ view: V) {
let item = NavigationItem(view)
viewStack.append( currentView )
withAnimation {
self.navigate = .forward
}
//>>>>>>>> HERE'S THE PROBLEM <<<<<<<<<
// ↓this↓ causes the Archive to fail.
currentView = item
}
func home() {
currentView = NavigationItem( HomeeView() )
viewStack.removeAll()
withAnimation {
self.navigate = .root
}
}
func bringToFront<V: View & Navigatable>(_ view: V) {
let item = NavigationItem(view)
viewStack = viewStack.filter { $0 != item }
//advance(view)
}
}
struct NavigationItem: Equatable{
var view: AnyView
let id: String = UUID().uuidString
var description: String
init< V: View & Navigatable >(_ view: V, description: String? = nil) {
self.view = AnyView(view)
if description==nil {
self.description = V.getDescription(view)()//"\(type(of: view))"
} else {
self.description = description!
}
}
static func == (lhs: NavigationItem, rhs: NavigationItem) -> Bool {
return lhs.description == rhs.description
}
}
protocol Navigatable {
var description: String { get }
}
extension Navigatable {
func getDescription() -> String {
return "\(type(of: self))"
}
}
I've also asked a question regarding NavigationStack
previously, but a different issue.
I've sent a bug report via Feedback Assistant 2 weeks ago, but haven't gotten anywhere yet.
I'm in a rush to deploy a TestFlight, so any help is much appreciated!
Edit: Here's a part of the error message from the Archive failure:
...
1. While running pass #41789 SILFunctionTransform "GenericSpecializer" on SILFunction "@$s8okan_app15NavigationStackC7advanceyyx7SwiftUI4ViewRzAA11NavigatableRzlFAA04NextH0V_Tg5".
for 'advance(_:)' (at /Users/myname/Downloads/my-project/my-project/Navigations/NavigationStack.swift:48:2)
0 swift 0x0000000112c88a63 PrintStackTraceSignalHandler(void*) + 51
1 swift 0x0000000112c88236 SignalHandler(int) + 358
2 libsystem_platform.dylib 0x00007fff6627942d _sigtramp + 29
3 libsystem_platform.dylib 0x0000000800000001 _sigtramp + 2581097457
4 swift 0x000000010f0cd08c swift::ReabstractionInfo::prepareAndCheck(swift::ApplySite, swift::SILFunction*, swift::SubstitutionMap, swift::OptRemark::Emitter*) + 572
5 swift 0x000000010f11fc5a swift::ReabstractionInfo::ReabstractionInfo(swift::ApplySite, swift::SILFunction*, swift::SubstitutionMap, swift::IsSerialized_t, bool, swift::OptRemark::Emitter*) + 122
6 swift 0x000000010f129cb5 swift::trySpecializeApplyOfGeneric(swift::SILOptFunctionBuilder&, swift::ApplySite, llvm::SmallSetVector<swift::SILInstruction*, 8u>&, llvm::SmallVectorImpl<swift::SILFunction*>&, swift::OptRemark::Emitter&) + 1653
7 swift 0x000000010f0027b1 (anonymous namespace)::GenericSpecializer::run() + 2673
8 swift 0x000000010ef7bfbe swift::SILPassManager::execute() + 4606
9 swift 0x000000010ebda9eb swift::CompilerInstance::performSILProcessing(swift::SILModule*, swift::UnifiedStatsReporter*) + 6379
10 swift 0x000000010e8d2f45 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 33925
11 swift 0x000000010e8c72b4 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 6820
12 swift 0x000000010e8547b3 main + 1219
13 libdyld.dylib 0x00007fff660807fd start + 1
14 libdyld.dylib 0x00000000000000d2 start + 2583165142
error: Segmentation fault: 11 (in target 'my-project' from project 'my-project')