作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想拆分一个数组,如下所示。
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/
我是一名优秀的程序员,十分优秀!