首页 > 解决方案 > 线程 1:EXC_BAD_ACCESS(代码=1,地址=0x879bb6700)(CoreData)

问题描述

我正在使用 CoreData 在另一个 ViewController 中使用一组数据预先填充 TableView(我有 Right Detail 单元格类型)。当我实际转到 TableView 时,它崩溃并给我错误“线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x879bb6700)”。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "SubCell", for: indexPath)
        
    let mSub = self.mSubs![indexPath.row]
    cell.textLabel?.text = mSub.newWord?.lowercased()
    cell.detailTextLabel?.text = mSub.oldWord?.lowercased()
        
    return cell
}

真正让我困惑的是,只有在我预先填充 newWord 数据时才会发生错误。oldWord 数据永远不会崩溃(即使我将 oldWord 设置为保存 newWord 数据以进行调试,反之亦然)。这是我预先填充数据的地方:

let launchedBefore = UserDefaults.standard.bool(forKey: "launchedBefore")
if launchedBefore  {
    print("Not first launch.")
} else {
    print("First launch, setting UserDefault.")
    UserDefaults.standard.set(true, forKey: "launchedBefore")
            
    let ogOldWords = ["equals", "plus", "minus", "subtracted by", "negative", "times", "x", "multiplied by", "over", "divided by", " squared", "squared", "square root of ", "square root ", "cube root of ", " cubed", "cubed", " to the ", "to the ", "open parentheses", "close parentheses", "zero", "one", "two", "three", "four", "five", "six", "sax", "sacks", "seven", "eight", "nine", "why", "ask", "eggs", "equal", "for", "too", "to", "/", "d1"]
            
    let ogNewWords = ["=", "+", "-", "-", "-", "⋅", "⋅", "⋅", "⋅", "/", "/", "^2", "^2", "√", "∛", "^3", "^3", "^", "^", "( ", " )", "0", "1", "2", "3", "4", "5", "6", "6", "6", "7", "8", "9", "Y", "X", "X", "=", "4", "2", "2", "/", "done"]
            
    for i in 0..<ogOldWords.count {
        let mSubO = MathSubs(context: globalVariables.context)
        mSubO.newWord = ogNewWords[i]
        mSubO.oldWord = ogOldWords[i]
    }

    do {
        try globalVariables.context.save()
    }
    catch {
        print("Could not save data")
    }
}

编辑: 我有时会遇到的另一个错误是“线程 1:”-[CALayer copyWithZone:]:无法识别的选择器发送到实例 0x6000029f72a0“”

编辑 2: 在处理应用程序的另一部分(在原始 ViewController 中)时,我再次遇到与 newWord 相同的错误。

do {
    mSubs = try globalVariables.context.fetch(MathSubs.fetchRequest())
    for i in 0..<mSubs!.count {
        mainString = mainString.replacingOccurrences(of: mSubs![i].oldWord ?? "", with: mSubs![i].newWord ?? "", options: .literal, range: mainString.startIndex..<mainString.endIndex)
    }
}
catch {
    print("Could not fetch data")
}

该应用程序在 newWord 而不是 oldWord 上崩溃。

标签: swiftuitableviewexc-bad-access

解决方案


推荐阅读