alexander Litvinov alexander Litvinov - 1 month ago 7
Swift Question

how to get Int from elements of dictionary array. xcode

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])")


I need elements [0] take unary operation: var = 150 + element [0]

Answer

This?

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)")

        }
    }
Comments