Home > Mobile >  How to put CFArray? values into a Swift Array in SwiftUI?
How to put CFArray? values into a Swift Array in SwiftUI?

Time:04-03

for my project I need to get a list of all the currencies and put them in a Picker, but with my solution I get a following error:

Binary operator '??' cannot be applied to operands of type 'CFArray?' and '[String]'

Please help me cast CFArray to Array of Strings or suggest another way to get all the ISO Currency keys.

The code:

import SwiftUI

struct ContentView: View {
    
    let symbolsArray = CFLocaleCopyISOCurrencyCodes() ?? ["USD"]
    @State private var selectedSymbol = "USD"
    
    var body: some View {
        NavigationView {
            Form {
                Picker("Select your currency", selection: $selectedSymbol) {
                    ForEach(symbolsArray, id: \.self) {
                        Text($0)
                    }
                }
            }
        }
    }

}

CodePudding user response:

Just cast it to array of strings, like

let symbolsArray = CFLocaleCopyISOCurrencyCodes() as? [String] ?? ["USD"]

Tested with Xcode 13.2 / iOS 15.2

demo1 demo2

  • Related