gpt4 book ai didi

ios - 如何在swift3中用其他元素替换数组的值?

转载 作者:行者123 更新时间:2023-12-01 17:28:43 24 4
gpt4 key购买 nike

我有一个数组A,其中包含2个数组:

[[Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday], [Monday, Wednesday]]

现在我想用以下命令替换数组A的元素:
[[Sun, Mon, Tue, Wed, Thu, Fri, Sat], [Mon, Wed]]

为此,我打算使用:
for days in operatingDays {
for weekdays in days
{
if (operatingDays[i] as? String == "Sun") {
operatingDays[i] = "Su"
} else if operatingDays[i] as? String == "Mon"{
operatingDays[i] = "Mo"
} else if operatingDays[i] as? String == "Tue"{
operatingDays[i] = "Tu"
} else if operatingDays[i] as? String == "Wed"{
operatingDays[i] = "We"
} else if operatingDays[i] as? String == "Thu"{
operatingDays[i] = "Th"
} else if operatingDays[i] as? String == "Fri"{
operatingDays[i] = "Fr"
} else if operatingDays[i] as? String == "Sat"{
operatingDays[i] = "Sa"
}
}
}

但是它显示了错误:
enter image description here
但是如何在Swift3中实现这一点将不胜感激。

最佳答案

var input = [["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"], ["Monday", "Wednesday"]]

var output = [[String]]()

for array in input {
output.append(array.map {String($0.characters.prefix(3))})
}

输出将包含 [["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"], ["Mon", "Wed"]]

关于ios - 如何在swift3中用其他元素替换数组的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45216641/

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