我有Car实体和价格实体,它具有一对多关系(每辆车的价格最低,特定年份的价格最高。价格实体跟踪年份和当年最低/最高价格) 。我从coredata获取汽车数据。由于价格在Car class中存储为NSSet,我会迭代每个价格并将它们存储在2D数组中,如下所示:
var prices = [[Double]]()
// This line of code is made to return only one car by predicate
let cars = try moc.executeFetchRequest(carData) as! [Car]
for c in cars {
for p in c.price! {
prices[p.id!.intValue][0] = p.lowest
prices[p.id!.intValue][1] = p.highest
}
}
我试图将价格实体的id值用作数组索引,但我得到"可选类型NSNumber的值未解包"错误。如您所见,它已经解开了。我该如何解决这个问题?