Ios 如何使用结构数组中的数据

Ios 如何使用结构数组中的数据,ios,ipad,Ios,Ipad,我想使用纬度和逻辑度值​​在结构数组中表示多段线 您可以在视图中使用json中的列表[1]。纬度值,但如果尝试使用上面覆盖func viewDidLoad()中的列表[1]。纬度值,则会出现错误。 如何将此值用作多段线的纬度和经度值 Swift来源 import UIKit import GoogleMaps class TableController: UIViewController, GMSMapViewDelegate , UITableViewDataSource, UITableV

我想使用纬度和逻辑度值​​在结构数组中表示多段线

您可以在视图中使用json中的列表[1]。纬度值,但如果尝试使用上面覆盖func viewDidLoad()中的列表[1]。纬度值,则会出现错误。 如何将此值用作多段线的纬度和经度值

Swift来源

import UIKit
import GoogleMaps

class TableController: UIViewController, GMSMapViewDelegate , UITableViewDataSource, UITableViewDelegate{
    @IBOutlet weak var viewMap: GMSMapView!

    @IBOutlet weak var listTableView: UITableView!

    //JSON담을 구조
    var list:[MyStruct] = [MyStruct]()

    struct MyStruct
    {
        var index = ""
        var ip = ""
        var latitude = ""
        var longitude = ""
        var zonename = ""

        init(_ index:String, _ ip:String, _ latitude:String, _ longitude:String, _ zonename:String)
        {
            self.index = index
            self.ip = ip
            self.latitude = latitude
            self.longitude = longitude
            self.zonename = zonename
        }
    }

    override func viewDidLoad() {

        super.viewDidLoad()

        // set initial location
        let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: 37.209037, longitude: 126.976370, zoom: 16.0)
        viewMap.camera = camera
        let count: Int = 0
        //폴리라인
        let path = GMSMutablePath()
        path.add(CLLocationCoordinate2DMake(37.209037, 126.976370))
        path.add(CLLocationCoordinate2DMake(37.211643, 126.972444))
        path.add(CLLocationCoordinate2DMake(37.211457, 126.972147))

        let polyline = GMSPolyline(path: path)
        polyline.strokeWidth = 6.0
        polyline.map = viewMap

        listTableView.dataSource = self
        listTableView.delegate = self

        get_data("http://192.168.0.11/fence.php")
     } 

    func get_data(_ link:String)
    {
        let url:URL = URL(string: link)!
        let session = URLSession.shared

        let request = URLRequest(url: url)

        let task = session.dataTask(with: request, completionHandler: {
            (data, response, error) in

            self.extract_data(data)

        })

        task.resume()
    }


    func extract_data(_ data:Data?)
    {
        let json:Any?

        if(data == nil)
        {
            return
        }

        do{
            json = try JSONSerialization.jsonObject(with: data!, options: [])
        }
        catch
        {
            return
        }

        guard let data_array = json as? NSArray else
        {
            return
        }


        for i in 0 ..< data_array.count
        {
            if let data_object = data_array[i] as? NSDictionary
            {
                if let data_index = data_object["index"] as? String,
                    let data_ip = data_object["Ip"] as? String,
                    let data_latitude = data_object["Latitude"] as? String,
                    let data_longitude = data_object["Longitude"] as? String,
                    let data_Zonename = data_object["ZoneName"] as? String
                {
                    list.append(MyStruct(data_index, data_ip, data_latitude, data_longitude, data_Zonename))
                }

            }
        }


        refresh_now()


    }

    func refresh_now()
    {
        DispatchQueue.main.async(
            execute:
            {
                self.listTableView.reloadData()

        })
    }



    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {

        return list.count
    }

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)


        cell.textLabel?.text = list[indexPath.row].zonename + " : " +  list[indexPath.row].latitude


        return cell
    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
导入UIKit
导入谷歌地图
类TableController:UIViewController、GMSMapViewDelegate、UITableViewDataSource、UITableViewDelegate{
@IBVAR视图映射:GMSMapView!
@IBVAR listTableView:UITableView!
//JSON담을 구조
变量列表:[MyStruct]=[MyStruct]()
结构MyStruct
{
var index=“”
var ip=“”
var latitude=“”
var longitude=“”
var zonename=“”
init(uu索引:String、ip:String、uu纬度:String、uu经度:String、uu区域名称:String)
{
self.index=索引
self.ip=ip
自纬度=纬度
self.longitude=经度
self.zonename=zonename
}
}
重写func viewDidLoad(){
super.viewDidLoad()
//设置初始位置
让摄像机:GMSCameraPosition=GMSCameraPosition.camera(纬度:37.209037,经度:126.976370,变焦:16.0)
viewMap.camera=camera
让计数:Int=0
//폴리라인
let path=GMSMutablePath()
添加路径(CLLocationCoordinate2DMake(37.209037126.976370))
添加路径(CLLocationCoordinate2DMake(37.211643126.972444))
添加路径(CLLocationCoordinate2DMake(37.211457126.972147))
设多段线=GMSPolyline(路径:path)
折线.strokeWidth=6.0
polyline.map=viewMap
listTableView.dataSource=self
listTableView.delegate=self
获取_数据(“http://192.168.0.11/fence.php")
} 
func get_数据(_链接:字符串)
{
让url:url=url(字符串:链接)!
让session=URLSession.shared
let request=URLRequest(url:url)
让task=session.dataTask(with:request,completionHandler:{
(数据、响应、错误)
self.extract_数据(数据)
})
task.resume()
}
func提取数据(u数据:数据?)
{
有吗?
如果(数据==nil)
{
返回
}
做{
json=尝试JSONSerialization.jsonObject(带:data!,选项:[])
}
抓住
{
返回
}
guard let data_array=json作为?NSArray else
{
返回
}
对于0中的i..Int
{
返回列表。计数
}
public func tableView(tableView:UITableView,cellForRowAt indexPath:indexPath)->UITableViewCell
{
let cell=tableView.dequeueReusableCell(带有标识符:“cell”,用于:indexath)
cell.textLabel?.text=list[indexPath.row].zonename+“:“+list[indexPath.row].latitude
返回单元
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
}

您能粘贴准确的错误吗?如果可能的话,试着把代码简化成一个简单的例子,如下所述:这将有助于发现问题。您应该能够在
self.extract\u data(data)
完成后立即使用它。您可以尝试将自己的完成处理程序发送到
get_data
,并在数据任务的完成处理程序中运行它。