如何交换字符串数组中的第一项和第二项?

时间:2018-12-11 07:07:58

标签: ios swift

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

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

任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:4)

这是一种更具实用性的方法:

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 :(得分:4)

您可能想使用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)

答案 2 :(得分: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)
}

答案 3 :(得分:1)

如果您的ordinateArray看起来像这样

[-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)
}
相关问题