Ios 如何在swift中从集合属性提取并插入到新集合模型中
我有Ios 如何在swift中从集合属性提取并插入到新集合模型中,ios,arrays,swift,swift2,Ios,Arrays,Swift,Swift2,我有\u userLocations:[CLLocation]集合存储用户地理数据,我想循环抛出\u userLocations集合,提取时间戳、纬度和经度属性,并插入名为savedLocations:[Location]=[]的模型集合。当我尝试循环使用for循环时,出现此错误\u userLocations “[Location]”类型的“值”没有成员“时间戳” 有人能帮我吗 我的模型 class Location { @NSManaged var latitude:
\u userLocations:[CLLocation]
集合存储用户地理数据,我想循环抛出\u userLocations集合,提取时间戳、纬度和经度属性,并插入名为savedLocations:[Location]=[]
的模型集合。当我尝试循环使用for循环时,出现此错误\u userLocations
“[Location]”类型的“值”没有成员“时间戳”
有人能帮我吗
我的模型
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
}
您得到类型“[Location]”的错误
值没有成员“timestamp”
的原因是因为您的savedLocations
是一个[Location]
数组
没有时间戳
属性
您需要做的是创建一个新的位置
对象并插入到保存的位置
for location in _userLocations {
let newLocation = Location()
newLocation.timestamp = location.timestamp
newLocation.latitude = location.coordinate.latitude
newLocation.longitude = location.coordinate.longitude
savedLocations.append(newLocation)
}
您的
savedLocations
a[位置]
?您需要创建一个新的Location
对象并插入savedLocations
是的,这是一个位置数组。您可以演示我的操作方法,因为我是swift新手