gpt4 book ai didi

arrays - 如何通过分隔符将数组对象拆分为两个(或二维数组)

转载 作者:搜寻专家 更新时间:2023-11-01 06:38:32 24 4
gpt4 key购买 nike

我想拆分一个数组,如下所示。

let arrayToSplit = ["Europe|#|France|#|Paris", "Europe|#|Italy|#|Rome", "America|#|USA|#|Washington", "America|#|Canada|#|Ottawa"]

进入

let firstArray = ["Europe", "Europe", "America", "America"]
let secondArray = ["France", "Italy", "USA", "Canada"]
let thirdArray = ["Paris", "Rome", "Washington", "Ottawa"]

通过分成这三个数组,我想将数据收入拆分并呈现在UITableView中。

最佳答案

不是最好的解决方案,但有预期的结果(提示在代码注释中):

let arrayToSplit = ["Europe|#|France|#|Paris", "Europe|#|Italy|#|Rome", "America|#|USA|#|Washington", "America|#|Canada|#|Ottawa"]
var firstArray = [String]()
var secondArray = [String]()
var thirdArray = [String]()

for element in arrayToSplit {

// new array with substrings divided by "|#|" e.g. ["Europe", "Europe", "America", "America"]
let newArray = element.componentsSeparatedByString("|#|")
firstArray.append(newArray[0])
secondArray.append(newArray[1])
thirdArray.append(newArray[2])
}

print("first array: \(firstArray)") // first array: ["Europe", "Europe", "America", "America"]
print("second array: \(secondArray)") // second array: ["France", "Italy", "USA", "Canada"]
print("third array: \(thirdArray)") // third array: ["Paris", "Rome", "Washington", "Ottawa"]

关于arrays - 如何通过分隔符将数组对象拆分为两个(或二维数组),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38476691/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com