如何将 json 中的双精度值转换并设置为函数

How to convert and set double values from json to function

我的问题是:使用 alamofire,我需要将坐标传递给位置变量中的函数 (longitude, latitude)。获取数据成功,但是无法添加到函数中。该函数在 MapViewController.swift 中称为“showSomeMarkers”。谢谢!

代码:

class MapViewController: UIViewController {
    
    @IBOutlet var tableView: UITableView!
    @IBOutlet var googleMapView: GMSMapView!
    
    fileprivate var coordinates = [Model]()
    
    
    @IBAction func dismissAction(_ sender: Any) {
        dismiss(animated: true, completion: nil)
    }
    
    func getValues() {
        Networking.shared.getAllValues(value: "/kyiv/places") { coordinates in
            self.coordinates = coordinates
            self.tableView.reloadData()
        }
    }
    
    func showMarker (position: CLLocationCoordinate2D) {
        let marker = GMSMarker()
        marker.position = position
        marker.title = "Kiev"
        marker.snippet = "Capital of Ukraine"
        marker.map = googleMapView
    }
    //THIS FUNCTION
    func showSomeMarkers () {
        let position = CLLocationCoordinate2D(latitude: 50 , longitude: 30)
        let marker = GMSMarker(position: position)
        marker.title = "Some title"
        marker.map = googleMapView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let camera = GMSCameraPosition(latitude: 50.45, longitude: 30.52, zoom: 5.7)
        googleMapView.camera = camera
        self.showMarker(position: googleMapView.camera.target)
        getValues()
        showSomeMarkers()
    }
}

extension MapViewController: UITableViewDelegate , UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        coordinates.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell" , for: indexPath) as! Cell
        let value = coordinates[indexPath.row]
        cell.configureValues(value: value)
        print(value)
        return cell
    }
}

型号:

class Model: NSObject {

    var name: String
    var lat: Double
    var lng: Double
    
    init(with modelDictionary: [String: Any]) {
        name = modelDictionary["name"] as? String ?? ""
        lat = modelDictionary["lat"] as? Double ?? 0.0
        lng = modelDictionary["lng"] as? Double ?? 0.0
    }
}

单元格:

class Cell: UITableViewCell {

    @IBOutlet var nameLabel: UILabel!
    
    var latitude: Double?
    var longitude: Double?
    var name: String?
        
        func configureValues(value: Model) {
            nameLabel.text = value.name
            latitude = value.lat
            longitude = value.lng
        }
}

因为我需要 Json 中的 1 个数据类型字符串用于 table 视图,所以我在单元格中配置值。请帮忙。谢谢!

将您的功能更改为

func showSomeMarkers (latitude: Double, longitude: Double) {
    let position = CLLocationCoordinate2D(latitude: latitude , longitude: longitude)
    //rest of code
}

并从 getValues

的闭包中调用它
func getValues() {
    Networking.shared.getAllValues(value: "/kyiv/places") { coordinates in
        self.coordinates = coordinates
        for coordinate in self.coordinates {
            showSomeMarkers(latitude: coordinate.lat, longitude: coordinate.lng)
        }
        self.tableView.reloadData()
    }
}