'How to put CFArray? values into a Swift Array in SwiftUI?
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)
}
}
}
}
}
}
Solution 1:[1]
Just cast it to array of strings, like
let symbolsArray = CFLocaleCopyISOCurrencyCodes() as? [String] ?? ["USD"]
Tested with Xcode 13.2 / iOS 15.2
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Asperi |


