I am saving some data I get from an Api with Core Data. Sometimes when I want to save the data an error occurs:
Thread 4: EXC_BAD_ACCESS (code=1, address=0x0)
in this line:
coin.name = ticker.value as? String
This is my full function:
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
}
}
After some research I found out that the error indicates there is a NULL pointer, but when I inspect the variable it is not null: