gpt4 book ai didi

ios - Swift 数组中的数组

转载 作者:行者123 更新时间:2023-11-30 13:30:29 27 4
gpt4 key购买 nike

我有一个包含 13 个项目的数组,如下所示。

var data =[“MonthFolder/", 
"MonthFolder/January",
"MonthFolder/February",
"MonthFolder/March",
"DayFolder/",
"DayFolder/Monday",
"DayFolder/Tuesday",
"DayFolder/Wednesday",
"DayFolder/Thursday",
"YearFolder/",
"YearFolder/2016",
"YearFolder/2015",
"YearFolder/2014"]

我正在尝试打印最终结果如下的数组

[("MonthFolder/January","MonthFolder/February","MonthFolder/March"), 
("DayFolder/Monday","DayFolder/Tuesday","DayFolder/Wednesday","DayFolder/Thursday"),
("YearFolder/2016", "YearFolder/2015”, "YearFolder/2014")]”

我已经做到了

var fileName  = [AnyObject]()
var allFiles = [AnyObject]()

for item in data{

let keyString = item.characters.last

if keyString != "/"{
fileName.append(item)
}
else if keyString == "/"{
if fileName.count > 0{
allFiles.append(fileName)
fileName = []
}
}
}

并得到输出

[("MonthFolder/January","MonthFolder/February","MonthFolder/March"), 
("DayFolder/Monday","DayFolder/Tuesday","DayFolder/Wednesday","DayFolder/Thursday”)]

缺少 allFiles 中的第三个对象,因为最后一次迭代的 else if 条件失败。我如何实现我想要的输出?谢谢。

最佳答案

通过这种方式你将得到预期的输出:

var fileName  = [AnyObject]()
var allFiles = [AnyObject]()

for item in data{

let keyString = item.characters.last

if keyString != "/"{
fileName.append(item)
}
else if keyString == "/"{
if fileName.count > 0{
allFiles.append(fileName)
fileName = []
}
}
}
if fileName.count != 0 {
allFiles.append(fileName)
fileName = []
}

希望这对你有帮助:)

关于ios - Swift 数组中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36615100/

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