You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

初始化InjuryView子类时触发LLDB错误:EXC_BAD_INSTRUCTION

解决Swift子类初始化时EXC_BAD_INSTRUCTION崩溃问题

嘿,作为Swift新手遇到这种初始化崩溃确实头疼,我帮你分析下问题所在,其实就是初始化流程里的两个小错误导致的:

问题根源拆解

  1. 未定义变量触发致命错误
    你在InjuryView的初始化方法最后写了self.title = title,但这里的title根本没在当前初始化方法的参数里定义过!Swift里引用未定义的变量会在运行时触发EXC_BAD_INSTRUCTION,这就是你看到的崩溃直接原因。

  2. 初始化顺序的小疏漏(辅助问题)
    虽然你调用了super.init,但要记住Swift的规则:必须先初始化子类自己的所有存储属性,再调用父类的初始化方法(你这里其实做到了,但未定义变量是更核心的崩溃点)。

修正后的完整代码

父类ECGView(格式调整更易读,逻辑不变)

class ECGView {
    var title: String
    var normalImage: UIImage
    var markedImageByComponents: [MarkedAnatomyComponent]
    var steps: [UIImage]
    var stepDescriptions: [String]
    var sectionNames: [[String]]
    var attribution: String
    var injuryList: [[InjuryView]]
    var numberOfPages: Int = 0
    var anatomyOverlay: Bool = false
    var currentStep: Int = 0
    var section: Bool = true

    init(title: String, 
         normalImage: UIImage, 
         markedImageByComponents: [MarkedAnatomyComponent] = [], 
         steps: [UIImage] = [], 
         stepDescriptions: [String] = [], 
         sectionNames: [[String]] = [], 
         attribution: String, 
         injuryList: [[InjuryView]] = []){
        self.title = title
        self.normalImage = normalImage
        self.markedImageByComponents = markedImageByComponents
        self.steps = steps
        self.stepDescriptions = stepDescriptions
        self.sectionNames = sectionNames
        self.attribution = attribution
        self.injuryList = injuryList
    }
}

子类InjuryView(修复初始化错误)

class InjuryView: ECGView{
    var injuryImage: UIImage
    var injuryComponents: [MarkedAnatomyComponent]
    var injuryTitle: String
    var shortInjuryTitle: String
    var injuryAttribution: String
    var overviewText: String
    var findingsText: String
    var managementText: String
    var otherTitle: String
    var otherText: String
    var normalToggle: Bool = false

    init(injuryImage: UIImage = #imageLiteral(resourceName: "Placeholder.jpg"), 
         injuryComponents: [MarkedAnatomyComponent], 
         injuryTitle: String = "", 
         shortInjuryTitle: String = "", 
         injuryAttribution: String, 
         overviewText: String, 
         findingsText: String, 
         managementText: String, 
         otherTitle: String = "", 
         otherText: String = ""){
        
        // 第一步:先初始化子类自己的所有存储属性
        self.injuryImage = injuryImage
        self.injuryComponents = injuryComponents
        self.injuryTitle = injuryTitle
        self.shortInjuryTitle = shortInjuryTitle
        self.injuryAttribution = injuryAttribution
        self.overviewText = overviewText
        self.findingsText = findingsText
        self.managementText = managementText
        self.otherTitle = otherTitle
        self.otherText = otherText
        
        // 第二步:调用父类初始化方法,这里把子类的injuryTitle和injuryAttribution传给父类
        super.init(title: injuryTitle, 
                   normalImage: #imageLiteral(resourceName: "Blank.png"), 
                   attribution: injuryAttribution)
        
        // 第三步:修改父类的属性或者做其他配置
        self.section = false
        // 如果之后需要修改title,直接用具体值或者已定义的变量,比如:
        // self.title = "自定义标题"
    }
}

新手小贴士

  • 写代码时多留意Xcode的红色警告,很多时候崩溃的原因编译器早就提示了,比如未定义变量会直接标红。
  • 记住Swift子类初始化的顺序:子类属性初始化 → 调用super.init → 调整父类属性或其他配置。

内容的提问来源于stack exchange,提问作者Nate

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 10:08:35