【问题标题】:how to extract from collection properties and insert in into new collection model in swift如何从集合属性中提取并快速插入新的集合模型
【发布时间】:2016-02-09 20:03:16
【问题描述】:

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

'类型'[Location]'的值没有成员'timestamp''

有人可以帮我吗?

我的模特

   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
        }

【问题讨论】:

  • 你的savedLocations不是[Location]吗?您需要创建一个新的Location 对象并插入到savedLocations
  • 是的,它是位置数组
  • 你能告诉我如何因为我是 swift 新手

标签: ios arrays swift swift2


【解决方案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)
}

【讨论】:

    猜你喜欢
    • 2015-02-06
    • 1970-01-01
    • 2011-10-16
    • 2015-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-28
    相关资源
    最近更新 更多