arrays - 无法快速加载类数组中的数组数据

标签 arrays swift class var

大家好,我想从 3 个数组中获取数据以创建 map 注释。但是我无法将这 3 个数组中的数据加载到一个类中。这是我的代码:

我的类(class)文件:

import MapKit
class Jobdata: NSObject, MKAnnotation {
    let title: String?
    let coordinate: CLLocationCoordinate2D
    init(title: String,  coordinate: CLLocationCoordinate2D) {
        self.title = title
        self.coordinate = coordinate
        super.init()
    }
}

这些是我的数组:

var jobNameST = [String]()
var jobLongitudeST = [Double]()
var jobLatitudeST = [Double]()

这是我的位置数组:

let jobLocations = [Jobdata(title: "test", coordinate: CLLocationCoordinate2D(latitude: 30.4692991035765, longitude:  -97.7660876))]

我想将这 3 个数组添加到我的位置数组中。

最佳答案

首先你需要创建位置模型 我希望所有数组都有相同数量的记录 如果没有请先检查一下

struct Location 
{
    var title: String?
    var latitude: Double?
    var longitude: Double?

    init(title: String, latitude: Double, longitude: Double) {
        self.title = title
        self.latitude = latitude
        self.longitude = longitude
    }
}

根据给定的标题、纬度和经度数组创建位置模型数组

    func getLocations() -> [Location] {

            var locations = [Location]()
            for (index, value) in jobNameST.enumerated() {
                let location = Location(title: value, latitude: jobLatitudeST[index], longitude: jobLongitudeST[index])
                locations.append(location)
            }
            return locations
   }

关于arrays - 无法快速加载类数组中的数组数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52013035/

相关文章:

javascript - 使用Java中的Map从字谜数组中查找唯一的单词

python - 过滤 numpy 数组的 numpy 数组

c - C 中的递归数组函数

arrays - 使用 mapfile 将输出保存到关联数组

python - 如何在Python中将方法作为属性调用?

html - 使用 Kanna、Swift 和 AlamoFire 获取 HTML 的隐藏字段

Swift:如何使用 sizeof?

arrays - 如何使类型化数组符合协议(protocol)

C++:具有用户定义类型的构造函数

c++ - 空类方法实现