如何将双精度值从 json 转换和设置为函数

问题描述

我的问题是:使用 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 个数据类型字符串用于表格视图,因此我在单元格中配置了值。请帮忙。谢谢!

解决方法

将您的功能更改为

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()
    }
}