Almazini
Almazini

Reputation: 1873

issue with passing array of dictionaries into function

Swift is new for me so I would appreciate your help.
I have an array:

let bankDict  = [
    ["bank": "b1", "currency": "c1", "buyrate": 0.11, "sellrate": 1.222, "officialrate": 2.15],
    ["bank": "b1", "currency": "c2", "buyrate": 3.11, "sellrate": 4.222, "officialrate": 5.15],
    ["bank": "b1", "currency": "c3", "buyrate": 7.11, "sellrate": 8.222, "officialrate": 9.15]]

I am trying to create a function which should return buyrate/sellrate/officialrate from my array bankDict.

So far I have created just a loop to test logic:

for dict in bankDict {
    let b = dict["bank"]!
    let c = dict["currency"]!

    if b == "b1" && c == "c2" {
        let br = dict["buyrate"]!
        let sr = dict["sellrate"]!
        let or = dict["officialrate"]!
        println("\(br) -> \(sr) -> \(or)")
    }
}

As expected returns me: 3.11 -> 4.222 -> 5.15

When I am trying to create function:

func showRates (bnk: String, crnc: String) -> (br: Double, sr: Double, or: Double) {
    for dict in bankDict {
        let b = dict["bank"]!
        let c = dict["currency"]!

        if b == bnk && c == crnc {
            let br = dict["buyrate"]
            let sr = dict["sellrate"]
            let or = dict["officialrate"]
            println("\(br) -> \(sr) -> \(or)")
        }

        return (br, sr, or)
    }
}

I get an error: use of unresolved identifier 'sr'

Upvotes: 0

Views: 88

Answers (2)

Marcus Rossel
Marcus Rossel

Reputation: 3258

It's an issue of scope. br, sr, and or were defined in the scope of the if-statement:

if b == bnk && c == crnc {
    let br = dict["buyrate"]
    let sr = dict["sellrate"]
    let or = dict["officialrate"]
    println("\(br) -> \(sr) -> \(or)")
}

Therefore they are only useable within that block of code.
If you move the return statement to be part of the if-statement it works (with a few other adjustments):

let bankDict  = [
    ["bank": "b1", "currency": "c1","buyrate": 0.11, "sellrate": 1.222, "officialrate": 2.15],
    ["bank": "b1", "currency": "c2","buyrate": 3.11, "sellrate": 4.222, "officialrate": 5.15],
    ["bank": "b1", "currency": "c3","buyrate": 7.11, "sellrate": 8.222, "officialrate": 9.15]]

func showRates(bank: String, currency: String) -> (buyrate: Double, sellrate: Double, officialrate: Double)? {
    for dict in bankDict {
        if dict["bank"] == bank && dict["currency"] == currency {
            if let buyrate = dict["buyrate"] as? Double,
               let sellrate = dict["sellrate"] as? Double,
               let officialrate = dict["officialrate"] as? Double {
                return (buyrate, sellrate, officialrate)
            }
        }
    }

    return nil
}

SwiftStub-Code (not the Swift 1.2 version yet)

Upvotes: 1

Zell B.
Zell B.

Reputation: 10286

Have you think what will happen if b is not equals to bnk or c to crnc? What should your function need to return in that case? If you want to return nil values then you can do the following

func showRates (bnk: String, crnc: String) -> (br: Double?, sr: Double?, or: Double?) {

for dict in bankDict {
    var b = dict["bank"]!
    var c = dict["currency"]!

    if b == bnk && c == crnc {
         var br = dict["buyrate"] as? Double
         var sr = dict["sellrate"] as? Double
         var or = dict["officialrate"] as? Double
         println("\(br) -> \(sr) -> \(or)")
         return (br, sr, or)
        }
}
return (nil, nil, nil)
}

Upvotes: 1

Related Questions