如何从集合属性中提取并插入到swift中的新集合模型中

时间:2016-02-09 20:03:17

标签: ios arrays swift swift2

我有_userLocations:[CLLocation]集合存储用户geoDatas,我想循环抛出_userLocations集合并提取时间戳,纬度和经度属性,并插入到名为savedLocations:[Location]=[]的模型集合中。当我尝试使用循环时for loop我有这个错误_userLocations

  

'类型的价值' [位置]'没有会员'时间戳'

有人可以帮助我吗?

我的模特

   class Location {

        @NSManaged var latitude: NSNumber?
        @NSManaged var longitude: NSNumber?
        @NSManaged var timestamp: NSDate?

    }

        for location in _userLocations {
            savedLocations.timestamp = location.timestamp
            savedLocations.latitude = location.coordinate.latitude
            savedLocations.longitude = location.coordinate.longitude
        }

1 个答案:

答案 0 :(得分:1)

您收到错误Value of type '[Location]' has no member 'timestamp'的原因是因为您的savedLocations[Location] Array没有timestamp属性

您需要做的是创建一个新的Location对象并插入savedLocations

for location in _userLocations {
    let newLocation = Location()
    newLocation.timestamp = location.timestamp
    newLocation.latitude = location.coordinate.latitude
    newLocation.longitude = location.coordinate.longitude

    savedLocations.append(newLocation)
}