如何重新运行将在字典中切换变量并再次运行其自身的函数?
Posted
技术标签:
【中文标题】如何重新运行将在字典中切换变量并再次运行其自身的函数?【英文标题】:How to Rerun a function that will switch variables in a dictionary and run its self again? 【发布时间】:2016-02-26 17:03:36 【问题描述】:嘿,我有 2 个简单的字符串和数字字典。我希望能够一次运行该函数能够再次运行它,但这次是 barcelonavsRealMadrid1goals2 而不是原来的。所以该函数基本上为第一个运行它,然后将 var goalCount 更改为第二个字典名称并自动再次运行它。看起来很简单任何帮助。谢谢! 代码:
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 目标计数,然后重新开始
【问题讨论】:
我还是不明白。你能详细说明一下吗? 【参考方案1】:您可以将变量作为参数传递给函数并以此为基础。
例如(仅供参考,未经测试):
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
)
【讨论】:
以上是关于如何重新运行将在字典中切换变量并再次运行其自身的函数?的主要内容,如果未能解决你的问题,请参考以下文章