簡體   English   中英

如何從集合屬性中提取並快速插入新的集合模型

[英]how to extract from collection properties and insert in into new collection model in swift

我有_userLocations:[CLLocation]集合存儲用戶geoDatas,我想循環拋出_userLocations集合並提取時間戳記,緯度和經度屬性,並插入到稱為savedLocations:[Location]=[]模型集合中。 for循環,我有此錯誤_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
        }

之所以出現錯誤Value of type '[Location]' has no member 'timestamp'的原因是,因為您savedLocations[Location][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)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM