我正在使用Core Data保存从Api获得的一些数据。有时当我要保存数据时会发生错误:
线程4:EXC_BAD_ACCESS(代码= 1,地址= 0x0)
在此行:
coin.name = ticker.value as? String
这是我的全部功能:
private func saveDictionary(version: ApiParameter, _ data: [String:Any?]?, _ fetchRequest: NSFetchRequest<Coin>) throws {
if checkVersion(version: version.versionKey()) {
if data != nil {
for ticker in data! {
if ticker.value != nil {
fetchRequest.predicate = NSPredicate.init(format: "ticker = %@", "\(ticker.key)")
do {
let coins = try managedContext.fetch(fetchRequest)
for coin in coins {
// print("ticker:",coin.ticker)
if ticker.key == coin.ticker {
switch version {
case .linksWebsites:
coin.website = ticker.value as? String
case .fdefSymbolsTallCc:
coin.ccsymbol = ticker.value as? String
case .icons32Exall:
coin.icon = ticker.value as? NSData
case .fdefCoinsTarrExall:
coin.group = ticker.value as? [String]
case .coinNamesExall:
coin.name = ticker.value as? String
default: break
}
}
}
}
}
}
}
}
do {
try managedContext.save()
} catch let error {
print(error.localizedDescription)
throw error
}
}
经过研究,我发现该错误表明存在NULL指针,但是当我检查该变量时,它不为null: