问题描述
我正在使用 CoreData 在另一个 ViewController 中用一组数据预填充 TableView(我有 Right Detail 单元格类型)。当我实际转到 TableView 时,它崩溃并给我错误“线程 1:EXC_BAD_ACCESS (code=1,address=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","√","∛","^3","^","( "," )","0","1","2","3","4","5","6","7","8","9","Y","X","=","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")
}
}
编辑: 我有时会遇到的另一个错误是“Thread 1:”-[CALayer copyWithZone:]: unrecognized selector sent to instance 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 上崩溃。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)