Tuesday, March 12, 2024
HomeiOS Developmentios - dataCorrupted(Swift.DecodingError.Context(codingPath: , debugDescription: "The given information was not legitimate JSON."

ios – dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: “The given information was not legitimate JSON.”


bumped into this downside, the identical plot on my macbook does not run and provides an error, however once I seemed with my good friend and he too compiled this venture to bounce the identical factor on macOs, it runs and works effective because it ought to. Has anybody run into this downside? The factor is, I’ve an api, I make a request, and once I click on on the button that makes my api request:

dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: “The given information was not legitimate JSON.”, underlyingError: Optionally available(Error Area=NSCocoaErrorDomain Code=3840 “Sudden character ‘S’ round line 1, column 1.” UserInfo={NSJSONSerializationErrorIndex=0, NSDebugDescription=Sudden character ‘S’ round line 1, column 1.})))

Right here is my code for you documentation

import SwiftUI import Mix import Basis

struct ContentView: View {
    @State personal var selectedTab = 0
    @State personal var searchText = ""
    @State personal var selectedOption = "Selectează opțiunea"
    @State personal var dynamicOptions: [String] = []

        
    let dayOfTheWeek = ["Luni", "Marti", "Miecuri", "Joi", "Vineri", "Sambata", "Duminica"]
    let weeks = [
        "1 (28.8.2023 - 3.9.2023)",
        "2 (4.9.2023 - 10.9.2023)",
        "3 (11.9.2023 - 17.9.2023)",
        "4 (18.9.2023 - 24.9.2023)",
        "5 (25.9.2023 - 1.10.2023)",
        "6 (2.10.2023 - 8.10.2023)",
        "7 (9.10.2023 - 15.10.2023)",
        "8 (16.10.2023 - 22.10.2023)",
        "9 (23.10.2023 - 29.10.2023)",
        "10 (30.10.2023 - 5.11.2023)",
        "11 (6.11.2023 - 12.11.2023)",
        "12 (13.11.2023 - 19.11.2023)",
        "13 (20.11.2023 - 26.11.2023)",
        "14 (27.11.2023 - 3.12.2023)",
        "15 (4.12.2023 - 10.12.2023)",
        "16 (11.12.2023 - 17.12.2023)",
        "17 (18.12.2023 - 24.12.2023)"
    ]
    
    let timeSlots = [
        "8:00 9:30",
        "9:45 11:15",
        "11:30 13:00",
        "13:15 14:45",
        "15:00 16:30",
        "16:45 18:15",
        "18:30 20:00"
    ]
    let staticOptions = ["Nu exista optiuni"]
    let searchOptions = ["Opțiune 1", "Opțiune 2", "Opțiune 3"]

    struct OfficesResponse: Decodable {
        let outcomes: [[String: String]]
    }
    
    struct Places of work: Decodable {
           let Id: String
           let Denumire: String
       }
    

    personal func fetchOfficesFromAPI() async throws -> [Offices] {
        let url = URL(string: "http://orar.usarb.md/api/entry/getOffices")!
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        
        let (information, _) = attempt await URLSession.shared.information(for: request)

        let places of work = attempt JSONDecoder().decode([Offices].self, from: information)
        
        return places of work
    }

    var physique: some View {
        ScrollView {
            VStack {
                headerImage
                
                VStack(spacing: 20) {
                    titleText
                    
                    HStack(spacing: 20) {
                        TabButton(title: "Grupuri", isSelected: selectedTab == 1) {
                            selectedTab = 1
                        }
                        
                        TabButton(title: "Profesori", isSelected: selectedTab == 2) {
                            selectedTab = 2
                        }
                        
                        TabButton(title: "Aule", isSelected: selectedTab == 3) {
                            Activity {
                                do {
                                    let places of work = attempt await fetchOfficesFromAPI()
                                    DispatchQueue.foremost.async {
                                        dynamicOptions = places of work.map { $0.Denumire }
                                        print("Opțiunile dinamice au fost actualizate.")
                                    }
                                } catch {
                                    print("A apărut o eroare la preluarea oficiilor: (error)")
                                }
                            }
                            selectedTab = 3
                        }


                    }
                    .padding(.horizontal)
                    
                    AutocompleteTextField(title: "Introducere textual content", choices: selectedTab == 3 ? dynamicOptions : staticOptions, textual content: $searchText)
                        .padding(.horizontal)
                        .cornerRadius(10)

                    
                    menuPicker(title: "Alege ziua", choices: dayOfTheWeek, selectedOption: $selectedOption)
                        .padding(.horizontal)
                    
                    menuPicker(title: "Alege săptămâna", choices: weeks, selectedOption: $selectedOption)
                        .padding(.horizontal)
                    
                    timeSlotRows
                        .padding(.horizontal)
                }
                buttonsRow
                    .padding(.horizontal)
                    .padding(.vertical, 10)

                infoText
            }
        }
    }
    
    personal var headerImage: some View {
        Picture("logo-orar")
            .resizable()
            .scaledToFit()
            .body(peak: 150)
    }
    
    personal var titleText: some View {
        Textual content("Orarul Cursurilor")
            .font(.title)
            .fontWeight(.daring)
            .body(maxWidth: .infinity)
            .padding()
            .background(
                RoundedRectangle(cornerRadius: 80)
                    .fill(LinearGradient(gradient: Gradient(colours: [Color.green.opacity(0.6), Color.green.opacity(0.4)]), startPoint: .main, endPoint: .trailing))
                    .shadow(radius: 5)
            ) .padding(.horizontal, 10)
    }
    
    personal var buttonsRow: some View {
        HStack(spacing: 15) {
            button("Descarca orar", shade: .inexperienced)
            button("Examene", shade: .purple)
            button("Planificare", shade: .purple)
        }
    }
    
    personal var infoText: some View {
        Textual content("Orarul nu lucrează între 23:00 și 3:00")
            .font(.headline)
            .fontWeight(.daring)
            .padding()
            .body(maxWidth: .infinity)
            .background(
                LinearGradient(gradient: Gradient(colours: [Color.green.opacity(0.2), Color.green.opacity(0.7)]), startPoint: .main, endPoint: .trailing)
                    .shadow(radius: 10)
                    .padding(.horizontal, 15)
            )
    }
    
    personal func menuPicker(title: String, choices: [String], selectedOption: Binding<String>) -> some View {
        MenuPicker(title: title, choices: choices, selectedOption: selectedOption)
            .padding(.horizontal)
    }
    
    personal var timeSlotRows: some View {
        VStack(spacing: 0) {
            ForEach(0..<7) { index in
                TimeSlotRow(timeSlot: timeSlots[index],
                            historyText: "Istoria românilor și universală",
                            seminarText: "Seminar",
                            professorText: "prof. Eremia Maria",
                            auditoriumText: "aud. 381")
                    .background(index % 2 == 0 ? Coloration(.systemGray6) : Coloration.clear)
            }
        }
    }
    
    personal func button(_ title: String, shade: Coloration) -> some View {
        Button(motion: {
            Activity {
                do {
                    let places of work = attempt await fetchOfficesFromAPI()

                    print(places of work)
                } catch {
                    print(error)
                }
            }
        }) {
            Textual content(title)
                .body(peak: 40)
                .body(maxWidth: .infinity)
                .background(shade.opacity(0.5))
                .foregroundColor(.black)
        }
    } }

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

struct TabButton: View {
    var title: String
    var isSelected: Bool
    var motion: () -> Void
    
    var physique: some View {
        Button(motion: motion) {
            Textual content(title)
                .font(.headline)
                .body(peak: 40)
                .body(maxWidth: .infinity)
                .background(isSelected ? Coloration.inexperienced.opacity(0.4) : Coloration.grey.opacity(0.2))
                .foregroundColor(.black)
                .cornerRadius(50)
        }
    } } struct AutocompleteTextField: View {
    var title: String
    var choices: [String]
    @Binding var textual content: String
    @State personal var isDropdownVisible = false
    @State personal var filteredOptions: [String] = []

    var physique: some View {
        VStack {
            TextField(title, textual content: $textual content, onEditingChanged: { isEditing in
                if isEditing {
                    self.isDropdownVisible = true
                }
            })
            .textFieldStyle(RoundedBorderTextFieldStyle())
            .padding(.horizontal)
            .cornerRadius(10)

            if isDropdownVisible {
                           VStack(alignment: .main, spacing: 0) {
                               ScrollView {
                                   ForEach(filteredOptions, id: .self) { choice in
                                       Button(motion: {
                                           self.textual content = choice
                                           self.isDropdownVisible = false
                                       }) {
                                           Textual content(choice)
                                               .foregroundColor(.black)
                                               .padding()
                                       }
                                   }
                               }
                               .body(maxHeight: 150)
                           }
                           .background(Coloration.white)
                           .cornerRadius(5)
                           .shadow(radius: 5)
                           .padding(.horizontal)
                           .padding(.backside, 4)
                       }
        }
        .onReceive(Simply(textual content)) { searchText in
            filteredOptions = choices.filter { $0.localizedCaseInsensitiveContains(searchText) }
            isDropdownVisible = !searchText.isEmpty
        }
    } }



struct MenuPicker: View {
    var title: String
    var choices: [String]
    @Binding var selectedOption: String
    
    var physique: some View {
        Picker(title, choice: $selectedOption) {
            ForEach(choices, id: .self) { choice in
                Textual content(choice)
            }
        }
        .pickerStyle(MenuPickerStyle())
        .body(width: 340, peak: 40)
        .background(LinearGradient(gradient: Gradient(colours: [Color.green.opacity(0.6), Color.green.opacity(0.4)]), startPoint: .main, endPoint: .trailing))
        .cornerRadius(30)
    } }

struct TimeSlotRow: View {
    var timeSlot: String
    var historyText: String
    var seminarText: String
    var professorText: String
    var auditoriumText: String
    
    var physique: some View {
        HStack {
            Textual content(timeSlot)
                .body(maxWidth: 50)
                .padding(.horizontal, 10)
                .padding(.vertical, 4)
                .background(Coloration.inexperienced)
                .foregroundColor(.white)
            Spacer().body(width: 20)
            VStack(alignment: .heart, spacing: 4) {
                Textual content(historyText)
                    .font(.system(dimension: 14))
                    .fontWeight(.black)
                
                Textual content(seminarText)
                    .font(.system(dimension: 14))
                    .fontWeight(.daring)
                
                Textual content(professorText)
                    .font(.system(dimension: 13))
                    .fontWeight(.daring)
                
                Textual content(auditoriumText)
                    .font(.system(dimension: 14))
            }
            .padding()
            Spacer()
        }
    } }



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments