Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dictionary as function return type

Tags:

ios

swift

I'm following a RW tutorial to learn about Swift and I'm getting errors at 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 type for function result
  • Consecutive declarations on a line must be separated by ';'
  • Expected declaration
  • Expected '{' in body of function declaration
like image 377
Carpetfizz Avatar asked Aug 10 '14 08:08

Carpetfizz


People also ask

Can a function return a dictionary?

Any object, such as dictionary, can be the return value of a Python function.

What is the return type of dictionary in Python?

Description. Python dictionary method type() returns the type of the passed variable.

Which function returns a list of dictionary?

Method 1: Get dictionary keys as a list using dict. The dict. keys() method in Python Dictionary, returns a view object that displays a list of all the keys in the dictionary in order of insertion.

Can you store functions in a dictionary Python?

Given a dictionary, assign its keys as function calls. Case 1 : Without Params. The way that is employed to achieve this task is that, function name is kept as dictionary values, and while calling with keys, brackets '()' are added.


1 Answers

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

you can update 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
}
like image 113
Alexey Globchastyy Avatar answered Oct 24 '22 09:10

Alexey Globchastyy