我有_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
}
答案 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)
}