【问题标题】:How do I swap the first and second items in an array of strings?如何交换字符串数组中的第一项和第二项?
【发布时间】:2018-12-12 18:45:22
【问题描述】:

我有一个字符串格式的坐标列表,分别具有经度和纬度。我想要纬度和经度:

var str = "POLYGON ((-97.7157864909859 30.406523563068, -97.7165355983688 30.4068866173208, -97.7167879301601 30.406495457696, -97.7168046574092 30.4065112735542, -97.7168203607564 30.406527859154, -97.7168349935064 30.4065451649171))

我已经去掉了它的格式字符串:

let formattedString = str.replacingOccurrences(of: "POLYGON ((", with: "").replacingOccurrences(of: "))", with: "").replacingOccurrences(of: ",", with: "")
var token = formattedString.components(separatedBy: " ")

现在我正在尝试翻转经度和纬度的位置,以便它:

30.406523563068, -97.7157864909859, 30.4068866173208, -97.7157864909859

我尝试了以下方法,但似乎没有改变数组的配置:

var coordinateArray = [String]()
for (index, coordinate) in token.enumerated() {
    if index % 2 == 0 {
        coordinateArray.insert(coordinate, at: index)
    } else {
        coordinateArray.insert(coordinate, at: index)
    }
}

任何帮助将不胜感激。

【问题讨论】:

  • 翻转位置没问题。但我建议制作一个位置模型并在其中存储经纬度。将信息存储在 Location 数组中并使用它。
  • 使用CoreLocation框架中的类来存储坐标。
  • 我不想存储坐标@RakeshaShastri
  • @Rizwan 我只是为了自己的目的尝试翻转坐标。我不需要模型。
  • @butter_baby "我有一个字符串格式的坐标列表" 嗯? o.O

标签: ios swift


【解决方案1】:

这是一个更实用的方法:

let str = "POLYGON ((-97.7157864909859 30.406523563068, -97.7165355983688 30.4068866173208, -97.7167879301601 30.406495457696, -97.7168046574092 30.4065112735542, -97.7168203607564 30.406527859154, -97.7168349935064 30.4065451649171))"
let formattedString = str.replacingOccurrences(of: "POLYGON ((", with: "").replacingOccurrences(of: "))", with: "")

// split by commas, not spaces, because in the original string, pairs of 
// coordinates are separated by commas
let coordianates = formattedString.components(separatedBy: ", ")

// Here I transform each pair of coordinate into an array with two elements, 
// reverse it, and flatten the whole array
let flipped = coordianates.flatMap { $0.components(separatedBy: " ").reversed() }
print(flipped)

【讨论】:

  • 我一直忘记这些函数式方法。很干净。谢谢+1
  • @Sweeper - 这不会完全反转整个字符串的顺序吗?
  • flatMap 内部的$0.components(separatedBy: " ") 是一个类似["-97.7157864909859", "30.406523563068"] 的数组。调用 reversed 只会反转这对坐标。 @Rizwan
  • @Sweeper - 是的,它应该像你解释的那样。但是当您运行此代码时,字符串中的最后一个位对位于数组中的第 0 和第 1 个索引处。字符串中的第一对 (-97.7157864909859 30.406523563068) 到最后。
  • @Rizwan 找出问题所在。现在修好了。 OP 删除了formattedString 中的所有逗号,导致拆分不起作用。
【解决方案2】:

您可能想尝试使用swapAt 函数,

var arr = [30.406523563068, -97.7157864909859, 30.4068866173208, -54.881838371711]
    for(index, _) in arr.enumerated() {
        if index % 2 == 0 && index + 1 < arr.count {
             arr.swapAt(index, index + 1)
        }
    }
print(arr)

【讨论】:

    【解决方案3】:

    使用swapAt(_:_:)

    示例:

    var names = ["Paul", "John", "George", "Ringo"]
    names.swapAt(0, 1)
    

    这会将数组中的"Paul""John" 交换。

    满足您的特定需求

    var str = "POLYGON ((-97.7157864909859 30.406523563068, -97.7165355983688 30.4068866173208, -97.7167879301601 30.406495457696, -97.7168046574092 30.4065112735542, -97.7168203607564 30.406527859154, -97.7168349935064 30.4065451649171))"
    
    let formattedString = str.replacingOccurrences(of: "POLYGON ((", with: "").replacingOccurrences(of: "))", with: "").replacingOccurrences(of: ",", with: "")
    var coordinateArray = formattedString.components(separatedBy: " ")
    
    for i in stride(from: 0, to: coordinateArray.endIndex - 1, by: 2) {
        coordinateArray.swapAt(i, i+1)
    }
    

    【讨论】:

      【解决方案4】:

      如果你的坐标数组是这样的

      [-97, 30, -97, 30]
      

      然后你可以说你想每两个元素滑动一次。然后你需要 stride 方法,它为你提供数组中元素的每一秒索引(从 0 开始),然后你可以使用 swapAt 方法,该方法将元素与第二个索引交换,元素与它之后的索引交换。

      for index in stride(from: coordinateArray.startIndex, to: coordinateArray.endIndex - 1, by: 2) {
           coordinateArray.swapAt(index, index + 1)
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-07
        • 1970-01-01
        • 1970-01-01
        • 2021-07-19
        • 2021-02-14
        • 1970-01-01
        • 2012-11-13
        相关资源
        最近更新 更多