从JSON,SwiftUI填充选择器

时间:2019-12-03 05:31:22

标签: json swift swiftui picker

有json(下面的链接),您需要将其中的数据放入选择器swiftui中,这可能吗?

这是加载必要数据的类。

class pickerAPI: ObservableObject {

    @Published var groupModel: GroupModel = [GroupModelElement]()

    init() {
        loadPickerData()
    }

    func loadPickerData() {
        guard let url: URL = URL(string: "https://gist.githubusercontent.com/lisindima/a3246c9eebae2e152c1f8211d10d4255/raw/30ee8647261b839c3a00024a851a340295300787/group") else { return }
        URLSession.shared.dataTask(with: url) { (data, response, error) in
            do {
                guard let json = data else { return }
                let swift = try JSONDecoder().decode(GroupModel.self, from: json)
                DispatchQueue.main.async {
                    self.groupModel = swift
                    print(self.groupModel)
                }
            }
            catch {
                print(error)
            }
        }
        .resume()
    }
}
public struct GroupModelElement: Codable, Hashable {
    public let startYear: Int?
    public let name: String?
    public let facultyID: String?
    public let specialityID: String?
    public let groupBr: Int?
    public let id: String?

    enum CodingKeys: String, CodingKey {
        case startYear
        case name
        case facultyID
        case specialityID
        case groupBr
        case id
    }

    public init(startYear: Int?, name: String?, facultyID: String?, specialityID: String?, groupBr: Int?, id: String?) {
        self.startYear = startYear
        self.name = name
        self.facultyID = facultyID
        self.specialityID = specialityID
        self.groupBr = groupBr
        self.id = id
    }
}

public typealias GroupModel = [GroupModelElement]

只有json中的“名称”字段才能进入选择器

let group: GroupModelElement
Picker(selection: $session.choiseGroup, label: Text("Выбранная группа")) {
                        ForEach(0 ..< group.name.count) {
                            Text(self.group.name[$0])
                        }
                    }

与该代码无关的其他地方都出现了错误。

1 个答案:

答案 0 :(得分:1)

问题是name是可选的。您必须先拆开包装才能使其正常工作。


struct ContentView: View {
    var elements:[GroupModelElement] = [
        GroupModelElement(startYear: 1990, name: "name1", facultyID: "1", specialityID: "2", groupBr: 3, id: "abc1"),
        GroupModelElement(startYear: 1991, name: "name2", facultyID: "10", specialityID: "20", groupBr: 30, id: "abc2"),
        GroupModelElement(startYear: 1992, name: "name3", facultyID: "100", specialityID: "200", groupBr: 300, id: "abc3")
    ]
    @State var selectedIndex = 0

    var body: some View {
        NavigationView {
            Form {
                Section {
                    Picker(selection: $selectedIndex, label: Text("elements")) {
                        ForEach(0 ..< elements.count) {
                            Text(self.elements[$0].name ?? "unknown")
                        }
                    }
                }
            }.navigationBarTitle("Select your name")
        }
    }
}

我可以建议您在GroupModelElement课堂上做些什么。为每个可选变量创建一个计算属性,如下所示:

public struct GroupModelElement: Codable, Hashable {
    public let startYear: Int?
    public var wrappedStartYear:Int{
        startYear ?? -1
    }
    public let name: String?
    public var wrappedName:String{
        name ?? "unknown name"
    }
    //...

然后,使用可选值更加容易,您可以在选择器Text(self.elements[$0].wrappedName)内进行调用。