Dictionary as a function of return type

I follow this RW to learn about Swift, and I get errors in the first line of the following function declaration:

func returnPossibleTips() -> [Int: Double] { let possibleTipsInferred = [0.15, 0.18, 0.20] let possibleTipsExplicit:[Double] = [0.15, 0.18, 0.20] var retval = [Int: Double]() for possibleTip in possibleTipsInferred { let intPct = Int(possibleTip*100) retval[intPct] = calcTipWithTipPct(possibleTip) } return retval } 

These are the errors:

  • Expected Function Result Type
  • Subsequent declarations in a line must be separated by a ';'
  • Expected Announcement
  • Expected '{' in function declaration text
+7
ios swift
source share
1 answer

It looks like you are not using the latest version of Swift (beta 5), ​​in the first versions there was no [Int] syntax for arrays.

you can upgrade Xcode or rewrite this code:

 func returnPossibleTips() -> Dictionary<Int, Double> { let possibleTipsInferred = [0.15, 0.18, 0.20] let possibleTipsExplicit:Array<Double> = [0.15, 0.18, 0.20] var retval = Dictionary<Int, Double>() for possibleTip in possibleTipsInferred { let intPct = Int(possibleTip * 100) retval[intPct] = calcTipWithTipPct(possibleTip) } return retval } 
+8
source share

All Articles