iOS Development

json – The right way to create knowledge tables with all columns displayed in Swift iOS?

Spread the love


I only recently began studying Swift 5 and located that my desk had only one column and never the remaining columns from my json knowledge. Please may anybody recommend learn how to show all of the columns for an information desk? Additionally, how can I search and kind a desk. I’ve used jQuery DataTables and I do not know learn how to construct these DataTables in Swift 5.

This is my code:
DataProvider.swift

import Basis
import SwiftUI

class DataProvider: ObservableObject {
    @Revealed var companies:[Company] = []
    let url: URL
    
    func getBusinessList(){
        if let url = Bundle.foremost.url(forResource: "firms", withExtension: "json") {
               do {
                   let knowledge = strive Knowledge(contentsOf: url)
                   let decoder = JSONDecoder()
                   let jsonData = strive decoder.decode([Company].self, from: knowledge)
                   self.companies = jsonData
               } catch {
                   print("error:(error)")
               }

           }
    }
    
    init(){
        self.url = URL(string: "http://localhost/output_json.php")!
        self.getBusinessList()
    }

}
struct Firm: Codable, Hashable, Identifiable {
    var id: Int { s_no }
    var firm, deal with, fax, telephone, web site: String
    var s_no: Int
}

ContentView.swift

import SwiftUI
import SwiftData

struct ContentView: View {
    @Surroundings(.modelContext) non-public var modelContext
    @EnvironmentObject var supplier: DataProvider
    
    var physique: some View {
        Desk( supplier.companies) {
            TableColumn("Title", worth:.firm)
                TableColumn("Web site", worth:.web site)
            }
            }
    }


#Preview {
    ContentView()
        .modelContainer(for: Merchandise.self, inMemory: true)
        .environmentObject(DataProvider())
}

ListViewerApp.swift

import SwiftUI
import SwiftData

@foremost
struct List_ViewerApp: App {
    var sharedModelContainer: ModelContainer = {
        let schema = Schema([
            Item.self,
        ])
        let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)

        do {
            return strive ModelContainer(for: schema, configurations: [modelConfiguration])
        } catch {
            fatalError("Couldn't create ModelContainer: (error)")
        }
    }()

    var physique: some Scene {
        WindowGroup {
            ContentView()
        }
        .modelContainer(sharedModelContainer)
        .environmentObject(DataProvider())
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *