NSOrderedSet不可转换为NSManagedObject

时间:2014-10-10 11:04:55

标签: ios core-data swift

我正在部分关注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)
    }

这是我的数据模型:

会话实体 Session Entity

位置实体 Location Entity

如果需要更多代码/信息来解决此问题,请告诉我。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您已将locations属性定义为“to-one”关系。这应该 是一个“多对多”的关系。然后,您可以指定NSSetNSOrderedSet, 取决于关系是否被定义为“有序”。