Swift:尝试测试 ObservedObject 变量是不是为 nil 时的 EXC_BREAKPOINT
Posted
技术标签:
【中文标题】Swift:尝试测试 ObservedObject 变量是不是为 nil 时的 EXC_BREAKPOINT【英文标题】:Swift: EXC_BREAKPOINT when trying to test if ObservedObject variable is nilSwift:尝试测试 ObservedObject 变量是否为 nil 时的 EXC_BREAKPOINT 【发布时间】:2020-12-14 22:15:51 【问题描述】:我喜欢测试变量是否为 nil,因为它是在建立与服务器的连接时设置的。 但是当我这样做时,我会收到一个 EXC_BREAKPOINT 代码=1。我不知道为什么。代码如下:
struct CategoryView: View
@ObservedObject var viewRouter: ViewRouter
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
var body: some View
NavigationView
if (viewRouter.menuStructure != nil)
CategoryPage(viewRouter: viewRouter, settingsShown: true, pageTitle: "Räume", items: viewRouter.menuStructure?.items ?? [])
.edgesIgnoringSafeArea(.top)
调试器中的 bt 显示如下:
* 线程 #1,队列 = 'com.apple.main-thread',停止原因 = EXC_BREAKPOINT(代码=1,子代码=0x1a1d79bd4) * 帧 #0:0x00000001a1d79bd4 SwiftUI`SwiftUI.ColumnNavigationView.BodyContent.body.getter:一些 + 944 帧 #1: 0x00000001a1ba22f4 SwiftUI`通用特化 SwiftUI.ViewBodyAccessor.updateBody(of: A, changed: Swift.Bool) -> () + 1276 框架 #2: 0x00000001a1cac218 SwiftUI`通用特化 >.0, SwiftUI.StaticBody>> 隐式闭包 #2 (Swift.UnsafeMutableRawPointer, __C.AGAttribute) -> () 在隐式闭包 #1 (A1.Type) -> (Swift .UnsafeMutableRawPointer, __C.AGAttribute) -> () 在闭包 #1 () -> (Swift.UnsafeMutableRawPointer, __C.AGAttribute) -> () 在闭包 #1 (Swift.UnsafePointer) -> AttributeGraph.Attribute 在 AttributeGraph.Attribute .init(A1) -> AttributeGraph.Attribute + 40 帧#3:0x00000001c39aabc0 AttributeGraph`AG::Graph::UpdateStack::update() + 492 帧 #4: 0x00000001c39aaff4 AttributeGraph`AG::Graph::update_attribute(AG::data::ptr, bool) + 332 帧 #5: 0x00000001c39b06f8 AttributeGraph`AG::Graph::input_value_ref_slow(AG::data::ptr, AG::AttributeID, unsigned int, AGSwiftMetadata const*, bool*, long) + 384 帧#6:0x00000001c39c272c AttributeGraph`AGGraphGetValue + 232 帧#7:0x00000001a1a5923c SwiftUI`通用专业化>,SwiftUI.MulticolumnSplitView>,SwiftUI.MulticolumnSplitView,Swift.AnyHashable,SwiftUI._VariadicView_Children.Element>>>>(SwiftUI 中的扩展):SwiftUI._ConditionalContent.(_1A625ACC143FD8528C59078F4 中的ChildView) content.getter : SwiftUI._ConditionalContent + 48 帧#8:0x00000001a1a59d78 SwiftUI`通用专业化>,SwiftUI.MulticolumnSplitView>,SwiftUI.MulticolumnSplitView,Swift.AnyHashable,SwiftUI._VariadicView_Children.Element>>>>(SwiftUI 中的扩展):SwiftUI._ConditionalContent.(_1A625ACC143FD8528C59078F4 中的ChildView) value.getter : SwiftUI.AnyView + 40 帧#9:0x00000001a1a7c230 SwiftUI`合并泛型专业化,SwiftUI._EnvironmentKeyWritingModifier>>,SwiftUI._EnvironmentKeyWritingModifier>>,SwiftUI._EnvironmentKeyWritingModifier>,SwiftUI._AlignmentLayout>,SwiftUI._PaddingLayout>,SwiftUI._PaddingLayout>,SwiftUI.SizeLimit.Layout _BackgroundModifier, SwiftUI.ModifiedContent, SwiftUI._FrameLayout>, SwiftUI._PushPopTransactionModifier>, SwiftUI._OffsetEffect>)>>>>, SwiftUI._BackgroundModifier, SwiftUI.ModifiedContent, SwiftUI._FrameLayout>, SwiftUI._PushPopTransactionModifier>, SwiftUI._OffsetEffect>)> >>>,SwiftUI._EnvironmentKeyWritingModifier>>,SwiftUI.ModifiedContent,SwiftUI._EnvironmentKeyWritingModifier>>,SwiftUI._EnvironmentKeyWritingModifier>>,SwiftUI._EnvironmentKeyWritingModifier>,SwiftUI._AlignmentLayout>,SwiftUI._PaddingLayout>,SwiftUI._PaddingLayout>,SwiftUI.SizeLimitLayout> , SwiftUI._BackgroundModifier, SwiftUI.ModifiedContent, SwiftUI._FrameLayout>, SwiftUI._PushPopTransactionModifier>, Swift UI._OffsetEffect>)>>>>, SwiftUI._BackgroundModifier, SwiftUI.ModifiedContent, SwiftUI._FrameLayout>, SwiftUI._PushPopTransactionModifier>, SwiftUI._OffsetEffect>)>>>>, SwiftUI._EnvironmentKeyWritingModifier>>, SwiftUI._EnvironmentKeyWritingModifier>>, SwiftUI._EnvironmentKeyWritingModifier>>>.(ChildView in _1A625ACC143FD8524C590782FD8F4F8C)> 隐式闭包 #2 (Swift.UnsafeMutableRawPointer, __C.AGAttribute) -> () 在隐式闭包 #1 (A1.Type) -> (Swift.UnsafeMutableRawPointer, __C. AGAttribute) -> () 在闭包 #1 () -> (Swift.UnsafeMutableRawPointer, __C.AGAttribute) -> () 在闭包 #1 (Swift.UnsafePointer) -> AttributeGraph.Attribute 在 AttributeGraph.Attribute.init(A1) -> AttributeGraph.Attribute + 32 帧 #10: 0x00000001a1a9fcd8 SwiftUI`部分应用转发器以进行泛型专业化>,SwiftUI.MulticolumnSplitView>,SwiftUI.MulticolumnSplitView,Swift.AnyHashable,SwiftUI._VariadicView_Children.Element>>>>。(_1A625ACC143FD8524C590782FD8F4F8C 中的ChildView 2(隐式关闭)> Swift.UnsafeMutableRawPointer, __C.AGAttribute) -> () 在隐式闭包 #1 (A1.Type) -> (Swift.UnsafeMutableRawPointer, __C.AGAttribute) -> () 在闭包 #1 () -> (Swift.UnsafeMutableRawPointer, __C.AGAttribute) -> () 在闭包 #1 (Swift.UnsafePointer) -> AttributeGraph.Attribute in AttributeGraph.Attribute.init(A1) -> AttributeGraph.Attribute + 28 第 11 帧:0x00000001c39aabc0 AttributeGraph`AG::Graph::UpdateStack::update() + 492 帧 #12: 0x00000001c39aaff4 AttributeGraph`AG::Graph::update_attribute(AG::data::ptr, bool) + 332 帧 #13: 0x00000001c39b06f8 AttributeGraph`AG::Graph::input_value_ref_slow(AG::data::ptr, AG::AttributeID, unsigned int, AGSwiftMetadata const*, bool*, long) + 384 第 14 帧:0x00000001c39c272c AttributeGraph`AGGraphGetValue + 232 帧 #15: 0x00000001a2069728 SwiftUI`SwiftUI.(AnyViewList in _A96961F3546506F21D8995C6092F15B5).updateValue() -> () + 60 帧#16:0x00000001a1cd0434 SwiftUI`部分应用转发器,用于隐式闭包#2(Swift.UnsafeMutableRawPointer,__C.AGAttribute)的通用特化->()隐式闭包#1(A1.Type)->(Swift.UnsafeMutableRawPointer,__C。 AGAttribute) -> () 在闭包 #1 () -> (Swift.UnsafeMutableRawPointer, __C.AGAttribute) -> () 在闭包 #1 (Swift.UnsafePointer) -> AttributeGraph.Attribute 在 AttributeGraph.Attribute.init(A1) -> AttributeGraph.Attribute + 24 帧 #17: 0x00000001c39aabc0 AttributeGraph`AG::Graph::UpdateStack::update() + 492 帧 #18: 0x00000001c39aaff4 AttributeGraph`AG::Graph::update_attribute(AG::data::ptr, bool) + 332 帧 #19: 0x00000001c39b06f8 AttributeGraph`AG::Graph::input_value_ref_slow(AG::data::ptr, AG::AttributeID, unsigned int, AGSwiftMetadata const*, bool*, long) + 384 帧 #20:0x00000001c39c272c AttributeGraph`AGGraphGetValue + 232 帧 #21: 0x00000001a1e85f74 SwiftUI`SwiftUI.(DynamicLayoutViewAdaptor in _8B9C7F39264416187A895085215951BC).updatedItems() -> Swift.Optional + 64 帧 #22: 0x00000001a1bc1fe0 SwiftUI`通用特化 SwiftUI.DynamicContainerInfo.(updateItems in _023AA827B8A8D39774F7A0C281455FEE)(disableTransitions: Swift.Bool) -> (更改: Swift.Bool, hasDepth: Swift.Bool) + 68 帧 #23: 0x00000001a1bc106c SwiftUI`通用特化 SwiftUI.DynamicContainerInfo.updateValue() -> () + 340 框架 #24: 0x00000001a1cb1514 SwiftUI`部分应用转发器用于通用专业化 > 隐式闭包 #2 (Swift.UnsafeMutableRawPointer, __C.AGAttribute) -> () 在隐式闭包 #1 (A1.Type) -> (Swift.UnsafeMutableRawPointer, __C .AGAttribute) -> () 在闭包 #1 () -> (Swift.UnsafeMutableRawPointer, __C.AGAttribute) -> () 在闭包 #1 (Swift.UnsafePointer) -> AttributeGraph.Attribute 在 AttributeGraph.Attribute.init(A1 ) -> AttributeGraph.Attribute + 24 帧 #25: 0x00000001c39aabc0 AttributeGraph`AG::Graph::UpdateStack::update() + 492 帧 #26: 0x00000001c39aaff4 AttributeGraph`AG::Graph::update_attribute(AG::data::ptr, bool) + 332 帧 #27: 0x00000001c39b06f8 AttributeGraph`AG::Graph::input_value_ref_slow(AG::data::ptr, AG::AttributeID, unsigned int, AGSwiftMetadata const*, bool*, long) + 384 帧 #28:0x00000001c39c272c AttributeGraph`AGGraphGetValue + 232 帧 #29: 0x00000001a1d37768 SwiftUI`SwiftUI.(DynamicPreferenceCombiner in _023AA827B8A8D39774F7A0C281455FEE).info.getter : Swift.Optional + 96 帧 #30: 0x00000001a1d37960 SwiftUI`SwiftUI.(DynamicPreferenceCombiner in _023AA827B8A8D39774F7A0C281455FEE).value.getter : A.Value + 204 帧 #31: 0x00000001a1ae2f9c SwiftUI`隐式闭包 #2 (Swift.UnsafeMutableRawPointer, __C.AGAttribute) -> () 在隐式闭包 #1 (A1.Type) -> (Swift.UnsafeMutableRawPointer, __C.AGAttribute) -> () in闭包 #1 () -> (Swift.UnsafeMutableRawPointer, __C.AGAttribute) -> () 在闭包 #1 (Swift.UnsafePointer) -> AttributeGraph.Attribute in AttributeGraph.Attribute.init(A1) -> AttributeGraph.Attribute + 124 帧 #32: 0x00000001c39aabc0 AttributeGraph`AG::Graph::UpdateStack::update() + 492 帧 #33: 0x00000001c39aaff4 AttributeGraph`AG::Graph::update_attribute(AG::data::ptr, bool) + 332 帧 #34: 0x00000001c39b06f8 AttributeGraph`AG::Graph::input_value_ref_slow(AG::data::ptr, AG::AttributeID, unsigned int, AGSwiftMetadata const*, bool*, long) + 384 帧 #35:0x00000001c39c272c AttributeGraph`AGGraphGetValue + 232 帧 #36: 0x00000001a1d37c14 SwiftUI`SwiftUI.(DynamicPreferenceCombiner in _023AA827B8A8D39774F7A0C281455FEE).value.getter : A.Value + 896 帧 #37: 0x00000001a1ae2f9c SwiftUI`隐式闭包 #2 (Swift.UnsafeMutableRawPointer, __C.AGAttribute) -> () in 隐式闭包 #1 (A1.Type) -> (Swift.UnsafeMutableRawPointer, __C.AGAttribute) -> () in闭包 #1 () -> (Swift.UnsafeMutableRawPointer, __C.AGAttribute) -> () 在闭包 #1 (Swift.UnsafePointer) -> AttributeGraph.Attribute in AttributeGraph.Attribute.init(A1) -> AttributeGraph.Attribute + 124 帧 #38: 0x00000001c39aabc0 AttributeGraph`AG::Graph::UpdateStack::update() + 492 帧 #39: 0x00000001c39aaff4 AttributeGraph`AG::Graph::update_attribute(AG::data::ptr, bool) + 332 帧 #40: 0x00000001c39b06f8 AttributeGraph`AG::Graph::input_value_ref_slow(AG::data::ptr, AG::AttributeID, unsigned int, AGSwiftMetadata const*, bool*, long) + 384 帧 #41:0x00000001c39c272c AttributeGraph`AGGraphGetValue + 232 帧 #42: 0x00000001a2009d90 SwiftUI`SwiftUI.(HostPreferencesTransform in _5EC2B8E51282B711F818D51701767C64).updateValue() -> () + 88 帧 #43: 0x00000001a1d3ec10 SwiftUI`部分应用转发器用于隐式闭包 #2 (Swift.UnsafeMutableRawPointer, __C.AGAttribute) -> () 在隐式闭包 #1 (A1.Type) -> (Swift.UnsafeMutableRawPointer, __C.AGAttribute) - > () 在闭包 #1 () -> (Swift.UnsafeMutableRawPointer, __C.AGAttribute) -> () 在闭包 #1 (Swift.UnsafePointer) -> AttributeGraph.Attribute 在 AttributeGraph.Attribute.init(A1) -> AttributeGraph .属性+32 帧 #44: 0x00000001c39aabc0 AttributeGraph`AG::Graph::UpdateStack::update() + 492 帧 #45: 0x00000001c39aaff4 AttributeGraph`AG::Graph::update_attribute(AG::data::ptr, bool) + 332 帧 #46: 0x00000001c39b0154 AttributeGraph`AG::Graph::value_ref(AG::AttributeID, AGSwiftMetadata const*, bool*) + 156 帧 #47:0x00000001c39c2774 AttributeGraph`AGGraphGetValue + 304 帧 #48: 0x00000001a236d5b8 SwiftUI`SwiftUI.GraphHost.updatePreferences() -> Swift.Bool + 56 帧 #49: 0x00000001a1e5b9d4 SwiftUI`SwiftUI.ViewGraph.updateOutputs(at: SwiftUI.Time) -> () + 112 帧 #50: 0x00000001a22c2eac SwiftUI`closure #1 () -> () in (SwiftUI 中的扩展):SwiftUI.ViewRendererHost.render(interval: Swift.Double, updateDisplayList: Swift.Bool) -> () + 1444 帧#51:0x00000001a22ba090 SwiftUI`(SwiftUI 中的扩展):SwiftUI.ViewRendererHost.render(间隔:Swift.Double,updateDisplayList:Swift.Bool)->()+ 340 帧 #52: 0x00000001a243ee00 SwiftUI`SwiftUI._UIHostingView.layoutSubviews() -> () + 176 帧 #53: 0x00000001a243ee34 SwiftUI`@objc SwiftUI._UIHostingView.layoutSubviews() -> () + 28 帧 #54: 0x000000019e383d90 UIKitCore`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2504 帧 #55: 0x000000019e899384 QuartzCore`-[CALayer layoutSublayers] + 308 帧 #56: 0x000000019e899858 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 524 帧 #57: 0x000000019e8adff8 QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 144 帧 #58: 0x000000019e7f3ee0 QuartzCore`CA::Context::commit_transaction(CA::Transaction*, double, double*) + 416 帧 #59: 0x000000019e81f36c QuartzCore`CA::Transaction::commit() + 732 帧 #60:0x000000019de55240 UIKitCore`__34-[UIApplication _firstCommitBlock]_block_invoke_2 + 84 帧#61:0x000000019b40449c CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 28 帧#62:0x000000019b4036e4 CoreFoundation`__CFRunLoopDoBlocks + 408 帧 #63: 0x000000019b3fdaa0 CoreFoundation`__CFRunLoopRun + 844 帧#64:0x000000019b3fd21c CoreFoundation`CFRunLoopRunSpecific + 600 帧 #65:0x00000001b247c784 GraphicsServices`GSEventRunModal + 164 帧 #66: 0x000000019de36200 UIKitCore`-[UIApplication_run] + 1072 帧 #67:0x000000019de3ba74 UIKitCore`UIApplicationMain + 168 帧 #68: 0x00000001a22fcb20 SwiftUI`closure #1 (Swift.UnsafeMutablePointer>>) -> Swift.Never in SwiftUI.(KitRendererCommon in _ACC2C5639A7D76F611E170E831FCA491)(Swift.AnyObject.Type) -> Swift.Never + 112 帧 #69: 0x00000001a22fcaac SwiftUI`SwiftUI.runApp(A) -> Swift.Never + 220 帧 #70:0x00000001a1e54e5c SwiftUI`static(SwiftUI 中的扩展):SwiftUI.App.main() -> () + 140 帧 #71: 0x0000000102377478 JarvisHome`static JarvisHomeApp.$main(self=JarvisHome.JarvisHomeApp) at :0 框架 #72: 0x0000000102379cf8 JarvisHome`main at JarvisHomeApp.swift:0 帧 #73: 0x000000019b0bd6c0 libdyld.dylib`start + 4有什么建议吗?如果我注释掉 if 行代码没有崩溃。
编辑:ViewRouter.swift
import Foundation
import Combine
import SwiftUI
class ViewRouter: ObservableObject
let objectWillChange = PassthroughSubject<ViewRouter, Never>()
var currentPage: String = "myhome"
didSet
objectWillChange.send(self)
var symcSocketStatus: String = "connecting"
didSet
objectWillChange.send(self)
var syncSocketStatusShown: Bool = true
didSet
objectWillChange.send(self)
var settingsShown: Bool = false
didSet
objectWillChange.send(self)
var menuStructure: MenuItem? = nil
didSet
objectWillChange.send(self)
编辑:MenuItem.swift
import Foundation
struct MenuItem: Codable
//var data: [String: Any]
var disabled: Bool
var hidden: Bool
var icon: String?
var id: Int
var ident: String
var info: String
var items: [MenuItem]?
var name: String
var parentId: Int?
var position: Int
var readOnly: Bool
var summary: String
var type: Int
init()
// self.data = ["": ""]
self.disabled = true
self.hidden = false
self.icon = ""
self.id = -1
self.ident = ""
self.info = ""
self.items = []
self.name = "Unknown"
self.parentId = 0
self.position = 0
self.readOnly = true
self.summary = ""
self.type = -1
【问题讨论】:
请分享 ViewRouter ViewRouter 在尝试编译时为我抛出一堆错误。 我的编译正常。我认为您的项目中缺少结构“MenuItem”。我会将其添加到我的问题中。 【参考方案1】:终于可以重新创建该问题.. 如果您的 if 返回 false,您在 NavigationView
内没有视图似乎是一个问题。只需添加具有任何视图的 else 案例即可解决此问题
struct ContentView: View
@ObservedObject var viewRouter: ViewRouter
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
var body: some View
NavigationView
if (viewRouter.menuStructure != nil)
CategoryPage(viewRouter: viewRouter, settingsShown: true, pageTitle: "Räume", items: viewRouter.menuStructure?.items ?? [])
else
Text("Menu Structure not loaded") //<< Some View where
.edgesIgnoringSafeArea(.top)
【讨论】:
非常感谢!我刚刚遇到了类似的问题,我的 NavigationView 中有非详尽的 if-else 子句,并且每次表达式不匹配时,我都会从 lldb 收到 EXC_BREAKPOINT 错误。我希望这篇文章能在未来挽救大量生命!以上是关于Swift:尝试测试 ObservedObject 变量是不是为 nil 时的 EXC_BREAKPOINT的主要内容,如果未能解决你的问题,请参考以下文章
SwiftUI - 从 Swift 类启动的可观察对象不会更新 ContentView() 上的 @ObservedObject
在 Swift 中使用 @observable,我想使用计时器定期刷新 ObservedObject。我该怎么做呢?
当 ObservedObject 在其他类中发生更改时如何在 ContentView 中运行方法 [Swift 5 iOS 13.4]
无法在真实 iPhone 上使用 @ObservedObject