Я занимаюсь разработкой приложения с использованием SwiftUI и Combine. Я сталкиваюсь с cra sh, который возникает, когда я пытаюсь включить / выключить темный режим пару раз. Журнал, напечатанный в консоли, гласит:
precondition failure: attribute failed to set an initial value: 78
В трассировке стека нет ценной информации, связанной с происхождением cra * sh.
* 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 и др. c.), но я не смог найти существенную информацию, чтобы понять причину этого кражи sh.
Большая часть работы была выполнена в классе 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?
}
Ниже приведены скриншоты пользовательского интерфейса:
Кто-нибудь сталкивался с этим? Если да, в чем была проблема и как вы ее решили?