Search code examples
arraysstringswiftuisplit

SwiftUI: bound string into array of strings


I have a variable "textEditorText" bound to the "TextEditor". When the button "send to the player" is clicked, the entire content of the text editor is transferred to the "stringArray" array. But in this array, all the text is contained as one element, and I need to split it into words. For example, the sentence "one, two, three" needs to be turned into the elements "one", "two", "three I'm trying to do this by passing the content from stringArray to stringArray2 but I'm getting the error messages

How can I overcome this situation? Thank you.

import SwiftUI

struct ContentView: View {
    
    @State var textEditorText:String = "one two three"
    
    @State var stringArray:[String] = []
    
    var stringArray2:[String] = stringArray.components(separatedBy: " ")
    
    var body: some View {
        NavigationView {
            
            VStack{
                TextEditor(text: $textEditorText)//binding to  @State var textEditorText
                    .frame(height: 200)
                    .cornerRadius(10)

                
                Button (action: {
                    textEditorText = ""
                    
                }, label: {
                    Text("clear the editor".uppercased())
                        .font(.headline)
                    //.foregroundColor(.white)
                        .padding(10)
                        .frame(maxWidth: .infinity)
                        .foregroundColor(.white)
                        .background(Color.blue)
                        .cornerRadius(10)
                })
                
                Button (action: {
                    if stringArray.isEmpty && !textEditorText.isEmpty {
                        stringArray.append(textEditorText)
                    }
                    
                }, label: {
                    Text("send to the player".uppercased())
                        .font(.headline)
                        .padding(10)
                        .frame(maxWidth: .infinity)
                        .foregroundColor(.white)
                    //.background(stringArray.isEmpty ? Color.red : Color.blue)
                        .background(Color.blue)
                        .cornerRadius(10)
                })
                
                Button(action: {
                    stringArray.removeAll()
                    
                }, label: {
                    Text("clear the player".uppercased())
                        .padding(10)
                        .frame(maxWidth: .infinity)
                        .background(Color.blue.cornerRadius(10))
                        .foregroundColor(.white)
                        .font(.headline)
                        .shadow(radius: 5)
                })
                
                
                ForEach(stringArray, id: \.self) { data in
                    Text(data)
                }
                
                .frame(maxWidth:.infinity, maxHeight: 30)
                Spacer()
                
            } //VStack
            .padding(10)
            .background(Color.gray.opacity(0.25))
          
            
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}



Solution

  • There's a lot of unnecessary code in your question, but if I understand correctly what you're trying to do, you just need to change it to use

    stringArray.append(contentsOf: )
    

    rather than

    stringArray.append()
    

    for example:

    if stringArray.isEmpty && !textEditorText.isEmpty {
        stringArray.append(contentsOf: textEditorText.components(separatedBy: " "))
    }