2016-02-26 38 views
0

嘿,我想弄清楚如何在45分鐘內打入球門的情況下計算出足球進球數,但是func有一些輕微的錯誤與迅速2.任何幫助?謝謝! enter image description here 代碼:如何在數字間隔的條件下製作一個「for」循環Xcode Swift2

var barcelonavsRealMadrid1goals : [String : Int] = ["barcelonaGoal1":21,"RealMadridGoal2":23,"barcelonaGoal3":24,"RealMadridGoal4":27] 


    func Run() { 
    var goalCount=0 
    for (goal,numbers) in barcelonavsRealMadrid1goals{ 
     for(var number in numbers) { 
      if(number < 45) 
      goalCount++ 

     } 
    } 
+0

什麼是錯誤,具體? –

+0

爲什麼你使用這種複雜的方法?你正在導致語法錯誤 –

+0

哦,好的,謝謝你的擡頭 – Hunter

回答

4

你有一個額外for..in循環在那裏,是沒有必要的:

for(var number in numbers) { 

它也有一個無關的()圍繞它

for var number in numbers { 

這裏是你的代碼的工作版本:

var barcelonavsRealMadrid1goals = ["barcelonaGoal1":21,"RealMadridGoal2":23,"barcelonaGoal3":24,"RealMadridGoal4":27] 

func run() -> Int { // functions should start with lower case 
    var goalCount=0 
    for (_,numbers) in barcelonavsRealMadrid1goals where numbers < 45 { 
    goalCount++ 
    } 
    return goalCount 
} 

let goalCount = run() 

和功能的方法是這樣的:

let goalCount = goals.reduce(0) { 
    if $0.1.1 < 45 { 
    return $0.0 + 1 
    } 
    return $0.0 
} 

並提供瞭解釋:

var goals = [ 
    "barcelonaGoal1" :21, 
    "RealMadridGoal2":23, 
    "barcelonaGoal3" :24, 
    "RealMadridGoal4":27, 
    "RealMadridGoal5":45] 

// For our use reduce takes an initial value of Int 
// and a combine function of type 
// (Int, (String, Int)) -> Int 
// 
// Reduce will call the closure once with 
// each value in the map and the previous return value 
let goalCount = goals.reduce(0, combine: { 
    (initial:Int, current:(key:String, value:Int)) -> Int in 
    var currentCount = initial 

    // print to show input and output of closure 
    print("parameters:(\(initial), (\"\(current.key)\", \(current.value)))", terminator:", ") 
    defer { 
    print("return:\(currentCount)") 
    } 
    // end printing 

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

// console output: 
// parameters:(0, ("barcelonaGoal1", 21)), return:1 
// parameters:(1, ("RealMadridGoal4", 27)), return:2 
// parameters:(2, ("RealMadridGoal5", 45)), return:2 
// parameters:(2, ("RealMadridGoal2", 23)), return:3 
// parameters:(3, ("barcelonaGoal3", 24)), return:4 
+1

很高興幫助!如果你學習它,功能性的方式非常棒,它可以真正簡化你的代碼。函數'map','filter'和'reduce'非常強大。 – ColGraff

+1

@Hunter我在關閉時添加了更多的細節,所以你可以研究它,如果你想。 – ColGraff

+0

哇看起來很棒!謝謝! – Hunter

0

爲你解決是問題,嘗試使用在迅速推出功能編程:

var barcelonavsRealMadrid1goals : [String : Int] = ["barcelonaGoal1":95,"RealMadridGoal2":23,"barcelonaGoal3":24,"RealMadridGoal4":27] 

var filtered = barcelonavsRealMadrid1goals.filter { (team:String, minute:Int) -> Bool in 

    var state = false 
    if (minute > 45) 
    { 
     return true 
    } 
    return state 
} 


let totalCount = filtered.count 
0

試試這個方法。

func Run() { 
    var goalCount=0 
    for (_, score) in barcelonavsRealMadrid1goals { 
      if(score < 45) { 
       goalCount++ 
      } 
     } 
    print(goalCount) 
}