Saturday, August 5, 2023
HomeiOS Developmentios - The right way to repair the error Three : "Try...

ios – The right way to repair the error Three : “Try and delete part containing first responder that refused to resign…” in Swift?


I am constructing app utilizing CoreData and on a stage of including knowledge I acquired an error when operating my app. This is the complete message: Thread 1: “Try and delete part containing first responder that refused to resign.nFirst responder that was requested to resign (returned YES from -resignFirstResponder): <UITextField: 0x130146000; body = (0 0; 249 22); opaque = NO; autoresize = W+H; gestureRecognizers = <NSArray: 0x600001df3750>; placeholder = Enter definition; borderStyle = None; background = <UITextFieldNoBackgroundProvider: 0x6000010e3620: textfield=<UITextField 0x130146000>>; layer = <CALayer: 0x60000122c240>> inside containing view: <SwiftUI.ListCollectionViewCell: 0x1341bec00; baseClass = UICollectionViewListCell; body = (20 35; 353 361); clipsToBounds = YES; layer = <CALayer: 0x600001238320>> at index path: <NSIndexPath: 0x833f455114c3dfbc> {size = 2, path = 0 – 0}nCurrent first responder: <TtGC7SwiftUI15CellHostingViewGVS_15ModifiedContentVS_14_ViewList_ViewVS_26CollectionViewCellModifier: 0x13107e200; body = (0 0; 353 361); autoresize = W+H; gestureRecognizers = <NSArray: 0x600001c333c0>; layer = <CALayer: 0x600001239ac0>> inside co”.

I suppose the difficulty is with the 2 arrays I’ve in DataConroller file or with the for loop which runs by way of dataController.termdefpairs when the “+” button is tapped.
The primary level of the this view is to have the ability to add knowledge for flashcards to have the ability to create a stack of them. Once I press the “save” button, I save the context and all of knowledge utilizing CoreData.
This is the view the place I enter the info (TermDefinitionView):

    struct TermDefinitionView: View {

    @ObservedObject var dataController = DataController.shared
    @ObservedObject personal var viewModel = TermDefinitionViewModel()
    @State var identify = ""
    @State var time period = ""
    @State var definition = ""
    @State var tag = ""// Separate state for the identify
    @Atmosphere(.managedObjectContext) var managedObjectContext
    @Atmosphere(.dismiss) var dismiss
    @State var showSet = false
    //@Binding var saveSet: Bool
    @State var isShowingSet = false
    @State personal var currentSet: SetEntity?
    //@Binding var redirectToSet: Bool
    
    var physique: some View {
        ZStack {
            NavigationStack {
                VStack {
                    Textual content("Title")
                        .padding(.trailing, 220)
                        .fontWeight(.daring)
                    TextField("Enter identify", textual content: $identify)
                        .body(width: 250)
                        .padding()
                        .overlay(RoundedRectangle(cornerRadius: 8).stroke(Coloration.grey, lineWidth: 1))
                    
                    Record {
                        
//                        ForEach($dataController.termdefpairs) { _ in
//                            TermView(time period: time period, definition: definition, tag: tag)
//
//                        }
                        
                        ForEach(dataController.termdefpairs.indices, id: .self) { index in
                            TermView(time period: dataController.termdefpairs[index].time period, definition: dataController.termdefpairs[index].definition, tag: dataController.termdefpairs[index].tag)

                        }
                        .onDelete { index in
                            self.dataController.termdefpairs.take away(at: index.first!)
                        }
                    }
                    .navigationBarItems(trailing: Button(motion: {
                        //for testForm in viewModel.termdefpairs {}
                        dataController.termdefpairs.removeAll()
                        dataController.save() //saving knowledge
                        
                            
                    }) {
                        Textual content("Save")
//                        
                        
                        
                    })
                    
                    Spacer()
                
                    Button(motion: {
                        dataController.addNew()
                        for pair in dataController.termdefpairs {
                            dataController.add(time period: pair.time period, definition: pair.definition, tag: pair.tag, date: Date(), identify: identify)
                        }
                        identify = ""
                        
                        isShowingSet = true
                        dismiss()
                    }) {
                        Picture(systemName: "plus")
                            .resizable()
                            .foregroundColor(.white)
                            .font(.title2)
                            .body(width: 30, peak: 30)
                    }
                    .body(width: 40, peak: 40)
                    .background(Coloration.blue)
                    .clipShape(Circle())
                    .padding()
                }
                
            }
        }
    
    }
}


struct TermDefinitionView_Previews: PreviewProvider {
    static var previews: some View {
        TermDefinitionView()
    }
}

struct TermView: View {
    @State var time period: String
    @State var definition: String
    @State var tag: String
    @State personal var isTagExpanded = false
    
    var physique: some View {
        VStack(alignment: .main, spacing: 20) {
            
            
                if isTagExpanded {
                    VStack(alignment: .main, spacing: 8) {
                        Textual content("Tag")
                            .font(.headline)
                        TextField("Enter tag", textual content: $tag)
                            .padding()
                            .overlay(RoundedRectangle(cornerRadius: 8).stroke(Coloration.grey, lineWidth: 1))
                    }
                }
                
                Button(motion: {
                    isTagExpanded.toggle()
                }) {
                    HStack { 
                        Spacer()
                        Picture(systemName: isTagExpanded ? "minus.circle.fill" : "plus.circle.fill")
                            .resizable()
                            .foregroundColor(.blue)
                            .body(width: 25, peak: 25)
                        Textual content(isTagExpanded ? "Cover Tag" : "Add Tag")
                            .foregroundColor(.blue)
                            .font(.headline)
                    }
                }
                .padding(.backside, -25)
            VStack(alignment: .main, spacing: 8) {
                Textual content("Time period")
                    .font(.headline)
                TextField("Enter time period", textual content: $time period)
                    .padding()
                    .overlay(RoundedRectangle(cornerRadius: 8).stroke(Coloration.grey, lineWidth: 1))
            }
            
            VStack(alignment: .main, spacing: 8) {
                Textual content("Definition")
                    .font(.headline)
                TextField("Enter definition", textual content: $definition)
                    .padding()
                    .overlay(RoundedRectangle(cornerRadius: 8).stroke(Coloration.grey, lineWidth: 1))
            }
            
            
        }
        .padding()
    }
}



struct TermAndDefinition: Identifiable {
    var id = UUID()
    var time period: String
    var definition: String
    var tag: String
}

DataController for managing knowledge:

    class DataController:  ObservableObject {
    static let shared = DataController()
    @Printed var termdefpairs: [TermAndDefinition] = []
    @Printed var savedFlash: [FlashCardData] = []
   
    let container: NSPersistentContainer

    init(inMemory: Bool = false) {
        container = NSPersistentContainer(identify: "CoreData")
        
        if inMemory {
            container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
        }
       // tremendous.init()
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                fatalError("Unresolved error (error), (error.userInfo)")
            }
            self.container.viewContext.mergePolicy = NSMergePolicy.mergeByPropertyObjectTrump
        })
        
        container.viewContext.automaticallyMergesChangesFromParent = true
        fetchRequest()
    }
    func addNew() {
        termdefpairs.append(TermAndDefinition(time period: "", definition: "", tag: ""))
//        func add(time period: String, definition: String, tag: String, date: Date, identify: String) {
//            let knowledge = FlashCardData(context: self.container.viewContext)
//            knowledge.id = UUID()
//            knowledge.definition = definition
//            knowledge.time period = time period
//            knowledge.tag = tag
//            knowledge.date = date
//            knowledge.identify = identify
//            fetchRequest()
//        }
    }
    func fetchRequest() {
        let request: NSFetchRequest<FlashCardData> = FlashCardData.fetchRequest()
                let type = NSSortDescriptor(keyPath: FlashCardData.date, ascending: false)
                request.sortDescriptors = [sort]

                do {
                    self.savedFlash = strive container.viewContext.fetch(request)
                } catch {
                    print("Didn't fetch FlashCardData: (error)")
                }
    }
    

    func save() {
        do {
            strive container.viewContext.save()
            print("Information saved")
        } catch {
            print("We couldn't save the info...")
        }
    }

    func add(time period: String, definition: String, tag: String, date: Date, identify: String) {
        let knowledge = FlashCardData(context: self.container.viewContext)
        knowledge.id = UUID()
        knowledge.definition = definition
        knowledge.time period = time period
        knowledge.tag = tag
        knowledge.date = date
        knowledge.identify = identify
        fetchRequest()
    }

CoreData:



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments