如何从字典数组的元素中获取Int。 Xcode中

时间:2016-10-23 12:42:17

标签: arrays swift xcode dictionary

let products = ["гречка" : "12,3,68", "рис" : "7,1,74", "овсянка" : "12,6,65"]

    let userInput = "р" 

    for pair in products { //
       if pair.key.contains(userInput) {
        let elements = pair.value.components(separatedBy: ",")
        print(pair.key + " contains  \(elements [0])")

我需要元素[0]采取一元操作:var = 150 + element [0]

1 个答案:

答案 0 :(得分:1)

此?

let products = ["гречка" : "12,3,68", "рис" : "7,1,74", "овсянка" : "12,6,65"]

    let userInput = "р"

    for pair in products { //
        if pair.key.contains(userInput) {
            let elements = pair.value.components(separatedBy: ",")
            let number = 150 + Int(elements[0])!
            print(pair.key + " contains  \(number)")

        }
    }