iosarraysxcodeswiftuichararray

How can I remove this "" from the character array?


This returned to me as a string:

result: vier­hundert­sieben­und­achtzig

I cast the string in Array.

self.randomNumberText = Array(self.numberConfig.numberString)
print("\(self.randomNumberText)")
["v", "i", "e", "r", "­", "h", "u", "n", "d", "e", "r", "t", "­", "s", "i", "e", "b", "e", "n", "­", "u", "n", "d", "­", "a", "c", "h", "t", "z", "i", "g"]

There are quotes("") that are hollow between the words. How can I remove these double quotes("")? The reason I cast the String in the Array: I check whether each letter entered from the TextField is spelled correctly. However, since there is "" this sign in between, I cannot check whether it is spelled correctly.

Number Config

class NumberConfig: ObservableObject {
    
    @Published var numberString: String = "-"

    @Published var randomNumber: Int = 0 {
        didSet {
            self.numberString = self.numberToText(number: randomNumber)
        }
    }
    
    func numberToText(number: Int) -> String {
        let formatter = NumberFormatter()
        formatter.locale = Locale(identifier: "de")
        formatter.numberStyle = .spellOut
        formatter.minimumIntegerDigits = 2
        let result = formatter.string(for: number) ?? "-"
        print("result: \(result)")
        return result
    }
}

View

    import Combine
    struct NumberToTextView: View {
        @ObservedObject var numberConfig = NumberConfig()
        @ObservedObject var textBindingManager = TextBindingManager(limit: 1)
        @ObservedObject var speechConfig = SpeechConfig()
        @State var randomNumber: [String.Element] = []
        @State var randomNumberText: [String.Element] = []
        @State var isCorrectAnswer: Bool = false
        @State var isTrueStep: Bool = false
        var body: some View {

....
    ZStack(alignment: .topLeading) {
                        
                        TextField("Sayıyı yazınız", text: $textBindingManager.text)
                            .padding()
                            .background(Color(UIColor.secondarySystemBackground))
                            .cornerRadius(15)
                            .onReceive(Just(self.textBindingManager.text), perform: { value in
                                let result = zip(self.randomNumberText, value).map({ $0 == $1 }).reduce(true, {$0 && $1})
                                if result && !self.textBindingManager.text.isEmpty {
                                    self.isTrueStep = true
                                    print("true")
                                    print("valeu: \(value) - \(self.randomNumberText)")
                                } else {
                                    self.isTrueStep = false
                                    print("falsee")
                                }
                            })
                            
                        Circle()
                            .foregroundColor(self.isTrueStep ? .green : .red)
                            .frame(width: 10, height: 10)
                            .padding(5)
                    }
                    .padding(.horizontal)
...
        }
    }

Solution

  • To filter the Character array is a bit tricky because an empty string isn't actually a character.

    Nevertheless there is a way: Filter all ASCII characters

    self.randomNumberText = Array(self.numberConfig.numberString).filter{$0.isASCII }
    

    or filter all letters

    self.randomNumberText = Array(self.numberConfig.numberString).filter{$0.isLetter }