【发布时间】:2014-10-10 11:04:54
【问题描述】:
我部分关注 Ray Wenderlich 的 this 教程(如果您搜索“保存运行”,它应该跳转到有关此问题的部分)。我也用“Swift”标记了这个,因为我不知道它是否会导致任何麻烦(因为它在 ObjC 中的教程中工作)。
我目前正坚持将位置数据保存到 CoreData 数据模型中。
这里是我的 saveSession 方法的代码:
func saveSession()
{
var newSession: Session = NSEntityDescription.insertNewObjectForEntityForName("Session", inManagedObjectContext: self.managedObjectContext!) as Session
newSession.timestamp = NSDate()
var tempArray = [AnyObject]()
for newLocation in self.locationsArray {
var location: Location = NSEntityDescription.insertNewObjectForEntityForName("Location", inManagedObjectContext: self.managedObjectContext!) as Location
location.timestamp = newLocation.timestamp
location.latitude = newLocation.coordinate.latitude
location.longitude = newLocation.coordinate.longitude
location.altitude = newLocation.altitude
location.heading = newLocation.heading
location.horizontalAccuracy = newLocation.horizontalAccuracy
location.verticalAccuracy = newLocation.verticalAccuracy
tempArray.append(location)
}
// This is the line producing the error:
// 'NSOrderedSet' is not convertible to 'NSManagedObject'
newSession.locations = NSOrderedSet(array: tempArray)
}
这是我的数据模型:
会话实体
位置实体
如果需要更多代码/信息来解决此问题,请告诉我。非常感谢任何帮助。
【问题讨论】: