Saturday, January 20, 2024
HomeiOS Developmentjson - create knowledge tables with all columns displayed in Swift...

json – create knowledge tables with all columns displayed in Swift iOS?


I only recently began studying Swift 5 and located that my desk had just one column and never the remaining columns from my json knowledge. Please might anybody recommend 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 construct these DataTables in Swift 5.

Here 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: "corporations", 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, handle, 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("Identify", 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())
    }
}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments