Why is Text Editor variable different from Text Field variable SwiftUI?

Viewed 68
import SwiftUI
import Foundation

func readLocalFile(forName name: String) -> Data? {
    do {
        if let bundlePath = Bundle.main.path(forResource: name,
                                             ofType: "json"),
           let jsonData = try String(contentsOfFile: bundlePath).data(using: .utf8) {
            return jsonData
        }
    } catch {
        print(error)
    }
    
    return nil
}

func serialization(data: Data) -> [String: String]? {
    let json = try! JSONSerialization.jsonObject(with: data, options: []) as? [String: String]
    return json
}

struct MainView: View {
    
    
    
    
    @State var takeInput: String = ""
    @State var textFieldArray: [String.SubSequence] = []
    @State var finalValues: [String] = []
    @State var showSheet: Bool = false
    
    
    
    
    
    
    var body: some View {
        let localData = readLocalFile(forName: "json_data")
        let dictionarydude = serialization(data: localData!)!
        
        
        let keys = dictionarydude.map{$0.key}
        
        VStack{
            TextField("Type details from your dream here", text: $takeInput)
            HStack{
            Button(action:{
                saveText()
                iterateText(keyParam: keys)
                showSheet.toggle()
                
            }, label: {
                Text("Submit".uppercased())
                    .font(.headline)
                    .foregroundColor(.white)
                    .padding()
                    .background(Color.blue)
                    .cornerRadius(10)
            })
                
                Button(action:{
                    takeInput = ""
                    textFieldArray = []
                    finalValues = []
                }, label: {
                    Text("Reset".uppercased())
                        .font(.headline)
                        .foregroundColor(.white)
                        .padding()
                        .background(Color.gray)
                        .cornerRadius(10)
                })
            }
        }
        .sheet(isPresented: $showSheet, content: {
            NextScreen(finalValues: finalValues, dictionarydude: dictionarydude)
        })
    }
    
    func iterateText(keyParam: [String]) {
        for i in textFieldArray{
            for dictkey in keyParam{
                if i.lowercased() == dictkey.lowercased(){
                    finalValues.append(dictkey)
                }
            }
        }
    }
    
    func saveText(){
        textFieldArray = Array(Set(takeInput.split(separator: " ")))
        takeInput = ""
    }

}

struct NextScreen: View {
    let finalValues: [String]
    let dictionarydude: [String: String]
    var body: some View{
        if finalValues.count > 0{
            NavigationView{
            ScrollView{
                VStack(alignment: .center, spacing: 20){
                    ForEach(finalValues, id: \.self) { index in
                        DisclosureGroup(content:{
                            
                            Text(dictionarydude[index]!)
                                .fontWeight(.bold)
                                .multilineTextAlignment(.leading)
                        },label:{
                            Text(index)
                                .font(.largeTitle)
                        })
                        .padding()
                    }
                }
                .navigationTitle("Meanings")
            }
            }
        }else{
            Text("Oops...")
                .font(.largeTitle)
                .fontWeight(.bold)
                .padding(10)
            Text("Your dream didn't contain any words with significant meanings")
                .italic()
                .fontWeight(.light)
        }
        
    }
}
import SwiftUI
import Foundation

func readLocalFile(forName name: String) -> Data? {
    do {
        if let bundlePath = Bundle.main.path(forResource: name,
                                             ofType: "json"),
           let jsonData = try String(contentsOfFile: bundlePath).data(using: .utf8) {
            return jsonData
        }
    } catch {
        print(error)
    }
    
    return nil
}

func serialization(data: Data) -> [String: String]? {
    let json = try! JSONSerialization.jsonObject(with: data, options: []) as? [String: String]
    return json
}

struct MainView: View {
    
    
    
    
    @State var takeInput: String = ""
    @State var textFieldArray: [String.SubSequence] = []
    @State var finalValues: [String] = []
    @State var showSheet: Bool = false
    
    
    
    
    
    
    var body: some View {
        let localData = readLocalFile(forName: "json_data")
        let dictionarydude = serialization(data: localData!)!
        
        
        let keys = dictionarydude.map{$0.key}
        
        VStack{
            TextEditor(text: $takeInput)
            HStack{
            Button(action:{
                saveText()
                iterateText(keyParam: keys)
                showSheet.toggle()
                
            }, label: {
                Text("Submit".uppercased())
                    .font(.headline)
                    .foregroundColor(.white)
                    .padding()
                    .background(Color.blue)
                    .cornerRadius(10)
            })
                
                Button(action:{
                    takeInput = ""
                    textFieldArray = []
                    finalValues = []
                }, label: {
                    Text("Reset".uppercased())
                        .font(.headline)
                        .foregroundColor(.white)
                        .padding()
                        .background(Color.gray)
                        .cornerRadius(10)
                })
            }
        }
        .sheet(isPresented: $showSheet, content: {
            NextScreen(finalValues: finalValues, dictionarydude: dictionarydude)
        })
    }
    
    func iterateText(keyParam: [String]) {
        for i in textFieldArray{
            for dictkey in keyParam{
                if i.lowercased() == dictkey.lowercased(){
                    finalValues.append(dictkey)
                }
            }
        }
    }
    
    func saveText(){
        textFieldArray = Array(Set(takeInput.split(separator: " ")))
        takeInput = ""
    }

}

struct NextScreen: View {
    let finalValues: [String]
    let dictionarydude: [String: String]
    var body: some View{
        if finalValues.count > 0{
            NavigationView{
            ScrollView{
                VStack(alignment: .center, spacing: 20){
                    ForEach(finalValues, id: \.self) { index in
                        DisclosureGroup(content:{
                            
                            Text(dictionarydude[index]!)
                                .fontWeight(.bold)
                                .multilineTextAlignment(.leading)
                        },label:{
                            Text(index)
                                .font(.largeTitle)
                        })
                        .padding()
                    }
                }
                .navigationTitle("Meanings")
            }
            }
        }else{
            Text("Oops...")
                .font(.largeTitle)
                .fontWeight(.bold)
                .padding(10)
            Text("Your dream didn't contain any words with significant meanings")
                .italic()
                .fontWeight(.light)
        }
        
    }
}

Hi everyone. Sorry I know this is a lot of code but I don't really have a way to make it smaller. The only things different in the two pieces are the lines containing either the TextField and the TextEditor. The problem is that the TextField version works and gives me the result in the picture if I just type in the word test, while the TextEditor passes an empty array into the NextScreen sheet and therefore triggers the else inside of the sheet as pictured here. If I swipe down the sheet and submit new text into the TextEditor without stopping and running the code again it works and the old inputs that would have been in the array show up as well. I would rather have a TextEditor in this case because I need multiple lines not just one that's why I've been trying to change this. I honestly have no clue why this doesn't work and I've gone through each variable and printed them and the only time anything shows up as an empty array and is different from the times when I used TextField is when it's passed to the sheet. I guess I just need to know if anyone knows of any differences between TextField and TextEditor that might cause this because I have spent a couple of hours trying to find something that says that they act differently but I can't find anything.

0 Answers
Related