gpt4 book ai didi

ios - 如何重新运行一个函数,该函数将切换字典中的变量并再次运行其自身?

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

嘿,我有 2 个简单的字符串和数字字典。我希望能够运行一次该函数,然后再次运行它,但这次是针对 barcelonavsRealMadrid1goals2 而不是原来的。因此该函数基本上为第一个字典运行它,然后将 var goalCount 更改为第二个字典名称并自动再次运行它的 self 。看起来很简单任何帮助。谢谢!代码:

var barcelonavsRealMadrid1 = [barcelonavsRealMadrid1goals, barcelonavsRealMadrid1penaltys]

var barcelonavsRealMadrid1goals : [String : Int] = ["barcelonaGoal0":14,"RealMadridGoal1":12,"barcelonaGoal2":29,"RealMadridGoal3":30]

var barcelonavsRealMadrid1goals2 : [String : Int] = ["barcelonaGoal0":14,"RealMadridGoal1":12,"barcelonaGoal2":29,"RealMadridGoal3":30]

func run() {

var goalCount = barcelonavsRealMadrid1goals.reduce(0, combine: {
(initial:Int, current:(key:String, value:Int)) -> Int in
var currentCount = initial
print("\(current.key)\", \((current.value))", terminator:", ")

defer {

}
if current.value <= 30 {
++currentCount // add 1 to the running total
return currentCount
}

return currentCount
// This is where i want to rerun the function but with the second dictionary now
goalCount = barcelonavsRealMadrid1(index + 1)
})

就像让 barcelonavsRealMadrid1 运行第一个索引槽,然后当 func 完成第一个索引槽时运行第二个索引槽,因此基本上更改了 var 目标计数,然后重新开始

最佳答案

您可以将变量作为参数传递给函数并以此为基础。

例如(仅供引用,未测试):

var barcelonavsRealMadrid1 = [barcelonavsRealMadrid1goals, barcelonavsRealMadrid1penaltys]
var barcelonavsRealMadrid1goals : [String : Int] = ["barcelonaGoal0":14,"RealMadridGoal1":12,"barcelonaGoal2":29,"RealMadridGoal3":30]
var barcelonavsRealMadrid1goals2 : [String : Int] = ["barcelonaGoal0":14,"RealMadridGoal1":12,"barcelonaGoal2":29,"RealMadridGoal3":30]

override func viewDidLoad() {
super.viewDidLoad()
run()
}

func run(goal:[String : Int]?) {
if let _goal = goal {
var goalCount = _goal.reduce(0, combine: {
(initial:Int, current:(key:String, value:Int)) -> Int in
var currentCount = initial;
print("KEY: \(current.key) VALUE: \(current.value)")

if current.value <= 30 {
++currentCount // add 1 to the running total
return currentCount
}

return currentCount
})
} else {
var goalCount = barcelonavsRealMadrid1goals.reduce(0, combine: {
(initial:Int, current:(key:String, value:Int)) -> Int in
var currentCount = initial;
print("KEY: \(current.key) VALUE: \(current.value)")

if current.value <= 30 {
++currentCount // add 1 to the running total
return currentCount
}

run(barcelonavsRealMadrid1goals2)
return currentCount
})
}
}

关于ios - 如何重新运行一个函数,该函数将切换字典中的变量并再次运行其自身?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35657602/

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