我正在使用SwiftUI和Combine开发一个应用程序。 我遇到几次尝试打开/关闭黑暗模式时发生的崩溃。 控制台中打印的日志显示:
precondition failure: attribute failed to set an initial value: 78
在堆栈跟踪中没有与崩溃起源有关的有价值的信息。
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
frame #0: 0x00000001ba5d9ec4 libsystem_kernel.dylib`__pthread_kill + 8
frame #1: 0x00000001ba4f9c00 libsystem_pthread.dylib`pthread_kill$VARIANT$armv81 + 192
frame #2: 0x00000001ba449844 libsystem_c.dylib`abort + 100
frame #3: 0x00000001e4eae548 AttributeGraph`AG::precondition_failure(char const*, ...) + 188
frame #4: 0x00000001e4e80194 AttributeGraph`AG::Graph::UpdateStack::update() + 1320
frame #5: 0x00000001e4e80308 AttributeGraph`AG::Graph::update_attribute(unsigned int, bool) + 372
frame #6: 0x00000001e4e83874 AttributeGraph`AG::Graph::input_value_ref_slow(unsigned int, unsigned int, AGTypeID, bool*) + 212
frame #7: 0x00000001f0ca08d8 SwiftUI`generic specialization <SwiftUI.Color._Resolved> of SwiftUI.AnimatableAttribute.update(context: inout AttributeGraph.AttributeContext<SwiftUI.AnimatableAttribute<A>>) -> () + 492
frame #8: 0x00000001f0bf7d80 SwiftUI`merged partial apply forwarder for generic specialization <SwiftUI.Color._Resolved> of + 20
frame #9: 0x00000001e4e80044 AttributeGraph`AG::Graph::UpdateStack::update() + 984
frame #10: 0x00000001e4e80308 AttributeGraph`AG::Graph::update_attribute(unsigned int, bool) + 372
frame #11: 0x00000001e4e84ff0 AttributeGraph`AG::Subgraph::update(unsigned int) + 548
frame #12: 0x00000001f093cde8 SwiftUI`SwiftUI.ViewGraph.(runTransaction in _D63C4EB7F2B205694B6515509E76E98B)(in: __C.AGGraphRef) -> () + 220
frame #13: 0x00000001f093d114 SwiftUI`closure #1 (__C.AGGraphRef) -> (prefs: Swift.Bool, idealSize: Swift.Bool, outputs: SwiftUI.ViewGraph.Outputs) in SwiftUI.ViewGraph.updateOutputs(at: SwiftUI.Time) -> () + 116
frame #14: 0x00000001f093ce9c SwiftUI`SwiftUI.ViewGraph.updateOutputs(at: SwiftUI.Time) -> () + 116
frame #15: 0x00000001f0c5a6b8 SwiftUI`closure #1 () -> () in closure #1 () -> () in (extension in SwiftUI):SwiftUI.ViewRendererHost.render(interval: Swift.Double, updateDisplayList: Swift.Bool) -> () + 960
frame #16: 0x00000001f0c5a120 SwiftUI`closure #1 () -> () in (extension in SwiftUI):SwiftUI.ViewRendererHost.render(interval: Swift.Double, updateDisplayList: Swift.Bool) -> () + 576
frame #17: 0x00000001f0c4f9a8 SwiftUI`(extension in SwiftUI):SwiftUI.ViewRendererHost.render(interval: Swift.Double, updateDisplayList: Swift.Bool) -> () + 400
frame #18: 0x00000001f0852c30 SwiftUI`closure #1 () -> () in SwiftUI._UIHostingView.requestImmediateUpdate() -> () + 68
frame #19: 0x00000001f084ed10 SwiftUI`reabstraction thunk helper from @escaping @callee_guaranteed () -> () to @escaping @callee_unowned @convention(block) () -> () + 24
frame #20: 0x0000000100eb97fc libdispatch.dylib`_dispatch_call_block_and_release + 24
frame #21: 0x0000000100ebabd8 libdispatch.dylib`_dispatch_client_callout + 16
frame #22: 0x0000000100ec8c34 libdispatch.dylib`_dispatch_main_queue_callback_4CF + 1316
frame #23: 0x00000001ba75f3a8 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
frame #24: 0x00000001ba75a39c CoreFoundation`__CFRunLoopRun + 2004
frame #25: 0x00000001ba7598a0 CoreFoundation`CFRunLoopRunSpecific + 464
frame #26: 0x00000001c46b1328 GraphicsServices`GSEventRunModal + 104
frame #27: 0x00000001be84a740 UIKitCore`UIApplicationMain + 1936
* frame #28: 0x0000000100a1452c WForcast`main at AppDelegate.swift:12:7
frame #29: 0x00000001ba5e4360 libdyld.dylib`start + 4
我已经浏览了很多在线讨论话题(Stackoverflow,Apple论坛等),但是我找不到足够的信息来了解崩溃的原因。
此错误开始出现时,大多数工作已在HomeView类中完成。我已经通过在上一次提交中测试代码来确认这一点。
以下是视图类。
struct HomeView: View {
@ObservedObject private(set) var viewModel: HomeViewModel
var body: some View {
VStack(alignment: .leading) {
HStack {
Image(systemName: "location")
.resizable()
.frame(width: 12, height: 12, alignment: .trailing)
.padding(.leading)
Text(viewModel.model.cityFullName)
.font(.headline)
.fontWeight(.light)
Spacer()
Circle()
.frame(width: 8, height: 8, alignment: .trailing)
.foregroundColor(viewModel.mode == .offline ? .red : .green)
Text(viewModel.mode.rawValue)
.font(.headline)
.fontWeight(.light)
.padding(.trailing)
}
.padding(.vertical)
List(viewModel.model.weatherDateMap) { item in
DailyForcastView(viewModel: DailyForcastViewModel(model: item))
Spacer()
}
}
}
}
以下是我的视图模型:
class HomeViewModel: ObservableObject {
@Published private(set) var model: CityWeatherModel
@Published private(set) var mode: AppMode = .offline
init(model: CityWeatherModel) {
self.model = model
}
}
以下是我的数据模型:
struct CityWeatherModel {
var weatherDateMap: [WeatherDateMap]
var city: City?
var cityFullName: String {
return city?.fullName ?? ""
}
}
struct WeatherDateMap: Identifiable {
let id: UUID = UUID()
let date: Date
let forcasts: [Forcast]
}
struct City: Decodable {
let id: Int?
let name: String?
let country: String?
var fullName: String {
get {
guard let name = self.name else { return "" }
return name + ((country != nil) ? ", \(country ?? "")" : "")
}
}
}
struct Forcast: Decodable, Identifiable {
let id: UUID = UUID()
let dateInterval: Int?
let date: Date?
var weatherParticular: WeatherParticular?
let weather: [Weather?]?
enum CodingKeys: String, CodingKey {
case dateInterval = "dt"
case date = "dtTxt"
case weatherParticular = "main"
case weather
}
}
struct WeatherParticular: Decodable {
var temp: Double?
let tempMin: Double?
let tempMax: Double?
let pressure: Double?
let seaLevel: Double?
let humidity: Double?
mutating func updateTemp(_ temprature: Double?) {
temp = temprature
}
}
struct Weather: Identifiable, Decodable {
let id: Int?
let main: String?
let description: String?
let icon: String?
}
以下是用户界面的屏幕截图:
有人遇到过这个吗? 如果是,那是什么问题,您是如何解决的?