Home > Software design >  SwiftUI replace and copy text TextEditor
SwiftUI replace and copy text TextEditor

Time:11-07

I have read countless articles and watched 10 hours of YouTube videos and still can't figure this out, maybe I'm just missing the term. I'm old school and still trying to learn this backwards swift stuff compared to vb or javascript.

I am using the default Mac OS document template in Xcode 13.1. it comes with a dialogue to open a file then dumps it into the TextEditor as utf8 string.

For my part I added another TextEditor and a button but keep getting error when I try to copy the text from the original texteditor to the new texteditor when button is pressed.

struct ContentView: View {
    
    @Binding var document: test2Document
    @State var newtexteditor: String = "press the button"
    
    var body: some View {
        VStack{
            
            //main text editor
            TextEditor(text: $document.text)
            //divider
            Divider()
            //new text editor
            TextEditor(text: $newtexteditor).padding()
            //button
            Button("test", action: dosomething)
        }
    }
    
    
    func dosomething () {   
        $newtexteditor = $document   
    }
}

Also, is there any ways of giving objects a name or label and using that to say things like textbox1.text = textbox2.text like you could using storyboards. I obviously know that's not swift syntax but you get the point.

CodePudding user response:

@State and @Binding vars are special kinds of variables. They actually contain 2 sub-variables: projectedValue and wrappedValue.

projectedValue is like a reference to the actual value, and allows the @State/@Binding to be modified from further down the view hierarchy.

  • You access this by saying $newTextEditor or $document.text.
  • This is only used for passing references down the view hierarchy, where you can later set the wrappedValue.

wrappedValue is the actual value — in your case, a String.

  • You access this by just saying newTextEditor or document.text. No $.
  • You're able to set this - for example, newTextEditor = "New text" or newTextEditor = document.text. The UI will automatically update to reflect the changes.

Here's an example:

struct ContentView: View {
    @State var text = "Hello"
    var body: some View {
        VStack {
            Text("The text is: \(text)") /// display the `wrappedValue`
            SubView(text: $text) /// pass in the `projectedValue`
        }
    }
}

struct SubView: View {
    @Binding var text: String
    var body: some View {
        Button("Click me to change text") {
            text = "New text" /// set the `wrappedValue`
        }
    }
}

In your code, it would look something like this:

struct ContentView: View {
    
    @Binding var document: Test2Document /// Side Note: structs like `Test2Document` should be Capitalized
    @State var newTextEditor: String = "press the button" /// also use camel case
    
    var body: some View {
        VStack{
            TextEditor(text: $document.text) /// pass in the `projectedValue`
            Divider()
            TextEditor(text: $newTextEditor) /// pass in the `projectedValue`
                .padding()
            Button("test", action: dosomething)
        }
    }
    
    func dosomething() {
        newTextEditor = document.text /// set the `wrappedValue`
    }
}
  • Related