как получить Int из элементов массива dictionary. xcode

#arrays #swift #xcode #dictionary

#массивы #swift #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])")
  

Мне нужно, чтобы элементы [0] выполняли унарную операцию: var = 150 элемент [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)")

        }
    }