首页 > 解决方案 > EXC_BAD_ACCESS with navigation bar title in SwiftUI

问题描述

I'm getting a crash on my iPhone (but not in simulator) whenever I include a navigation bar title in my SwiftUI view. If I take out the navigation bar title everything works just fine, but I need the title there. Here's my code:

NavigationView {
    List {
        ForEach(self.viewModel.tasks) { task in
            TaskRow(task: task)
        }
            .onDelete(perform: self.viewModel.delete(indexSet:))
    }
        .listStyle(.grouped)
        .edgesIgnoringSafeArea(.bottom)
        .navigationBarTitle(Text("mainTitle")) //EXC_BAD_ACCESS crash here
}

UPDATE

I stripped the code above down and started a new project to make sure it wasn't my custom UI or my business logic doing something weird. The code below produces the same crash on my iPhone.

struct ContentView : View {
    var body: some View {
        NavigationView {
            List {
                ForEach((0...10).identified(by: \.self)) { val in
                    Text("test")
                    }
                }
                .listStyle(.grouped)
                .edgesIgnoringSafeArea(.bottom)
                .navigationBarTitle(Text("Hey"))
        }
    }
}

Any idea why this is happening or how to fix it?

标签: iosswiftnavigationbarswiftui

解决方案


我解决了这个问题。我正在使用 Xcode 11 beta 1 构建,但我的 iPhone 运行的是 iOS 13 beta 2。由于 Swift 框架现在已内置到 iOS 中,因此我手机上的 Swift 框架(使用 beta 2 sdk)正在执行我的 iOS 应用程序(使用编译beta 1 sdk) 没有预料到。一旦我升级到 Xcode 11 beta 2,一切正常。


推荐阅读