【问题标题】:Swift: Print Coordinates from Firebase: 0,1 - 2,3 instead of 0,1 - 1,2Swift:从 Firebase 打印坐标:0,1 - 2,3 而不是 0,1 - 1,2
【发布时间】:2016-07-09 16:12:50
【问题描述】:

我正在尝试为 IndexPath.row 中的每一行打印位置,因为 IndexPath.row 确定他们希望在 TableView 中接收哪些特定内容。

来自 Firebase 的 NSDictionary 看起来像:x,y,x,y,x,y 等坐标。

0,1 = 第一个坐标 2,3 = 第二个坐标

Output: [55.617804300000003, 12.98939, 55.601572900000001, 12.979585399999999, 34.506667999999998, -81.948334000000003, -7.0909109999999993, 107.668887]

所以第一个0和1是x,y是第一个坐标,如下:

print(CLLocationCoordinate2D(latitude: openLocations[indexPath.row], longitude: openLocations[indexPath.row + 1]))

我们目前的结果如何:

x= longitude, y = altitude

x, y
0, 1
1, 2
2, 3
3, 4
5, 6

应该如何:

x= longitude, y = altitude

x, y
0, 1
2, 3
4, 5
6, 7
8, 9

NSDictionary 看起来像:

x,y,x,y,x,y ..

这应该是一件容易的事,但我现在的想法在其他地方,如果有任何人可以帮助我解决这个问题,我们将不胜感激。

提前致谢,

此致,

菲利普

【问题讨论】:

  • 你的字典看起来根本不像字典,更像是一个数组

标签: ios json swift firebase


【解决方案1】:

你必须每隔一行取一次:

latitude: openLocations[2 * indexPath.row], longitude: openLocations[2 * indexPath.row + 1]

【讨论】:

    【解决方案2】:

    实际上你拥有的是一个数组,你可以像这样循环遍历(假设你收到成对的位置):

    var openLocations = [55.617804300000003, 12.98939, 55.601572900000001, 12.979585399999999, 34.506667999999998, -81.948334000000003, -7.0909109999999993, 107.668887]
    for(var i = 0; i < openLocations.count; i += 2){
         let location = CLLocationCoordiante2D(latitude: openLocations[i], longitude: openLocations[i+1])
         print(location)
    }
    

    在使用 IndexPath 和 UITableView 的情况下,首先你有一些选择:

    选项 1: 首先将您从 firebase 收到的数据映射到绑定位置的数据结构。前任。它可以是一个 CLLocationCoordinate2D 数组,也可以是你自己想要的结构。

    选项 2: 您需要对数据源方法进行一些修改,假设:

     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
         return openLocations.count / 2
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-31
      • 1970-01-01
      • 1970-01-01
      • 2017-09-22
      • 1970-01-01
      • 1970-01-01
      • 2019-05-31
      相关资源
      最近更新 更多