SwiftUI JSON 到选取器

问题描述 投票:0回答:2

我正在尝试让“角色”在选择器中显示为文本。当我在选取器中使用通用文本视图时,它会像预期的那样显示 5 次,但我似乎无法从结构中提取名称。

JSON 数据:

[
{"roles": "Gorilla",
"index": 0},
{"roles": "Armadillo",
"index": 1},
{"roles": "Cheetah",
"index": 2},
{"roles": "Iguana",
"index": 3},
{"roles": "Monkey",
"index": 4}
]

型号:

import Foundation

struct inviteModel: Codable, Identifiable{
    
    let id = UUID()
    var roles:String
    var index:Int
    
}

查看:

@State var roleList = [inviteModel]()

 private func loadRoles(){
        
        Task {
            
            var ackval = "58ikf0"

            var request = URLRequest(url: URL(string: "MYURL")!)
            
            request.httpMethod = "POST"
            let parameters = "ack=\(ackVal)"
            request.httpBody = parameters.data(using: .utf8)
            
            let (data, response) = try await URLSession.shared.data(for: request as URLRequest)
            if (response as? HTTPURLResponse)?.statusCode == 200 {
                    
                do{
                    self.roleList = try JSONDecoder().decode([inviteModel].self, from: data)
                    }catch{
                    print(error)
                    }
                
                
            } else {
                print("API Error")
            }
            isSending = false
        }
        
    }

Picker(selection: $role1, label: Text("Role 1")) {
    ForEach(0..<roleList.count, id: \.self) { rlist in
        Text(rlist.roles).tag(0)
    }                                                
}

我确信这可能很简单,但我是 Swift 新手。

swiftui picker
2个回答
0
投票

尝试这种方法,将角色选择

role1
设为与 Picker 项目
.tag()
相同类型

struct InviteModel: Codable, Identifiable, Hashable {  // <-- here
    let id = UUID()
    var roles:String
    var index:Int
}

struct ContentView: View {
    @State var role1 = "Gorilla" // <-- here
    @State var roleList = [InviteModel]()
    
    private func loadRoles(){
        // for testing
        roleList = [InviteModel(roles: "Gorilla", index: 0),
                         InviteModel(roles: "Armadillo", index: 1),
                         InviteModel(roles: "Cheetah", index: 2),
                         InviteModel(roles: "Iguana", index: 3),
                         InviteModel(roles: "Monkey", index: 4)]
//        Task {
//            var ackval = "58ikf0"
//            var request = URLRequest(url: URL(string: "MYURL")!)
//            request.httpMethod = "POST"
//            let parameters = "ack=\(ackVal)"
//            request.httpBody = parameters.data(using: .utf8)
//            let (data, response) = try await URLSession.shared.data(for: request as URLRequest)
//            if (response as? HTTPURLResponse)?.statusCode == 200 {
//                do{
//                    self.roleList = try JSONDecoder().decode([inviteModel].self, from: data)
//                }catch{
//                    print(error)
//                }
//            } else {
//                print("API Error")
//            }
//            isSending = false
//        }
    }
    
    var body: some View {
        Picker("Role 1", selection: $role1) {
            ForEach(roleList) { invite in
                Text(invite.roles).tag(invite.roles)  // <-- here
            }
        }
        .onAppear {
            loadRoles()
        }
    }
}

-1
投票

更新:我通过查看上面提供的代码找到了答案。

问题是,我试图给 ForEach 循环一个范围,而不是使用内置的 Swift 功能来迭代数据模型。

Picker(selection: $role1, label: Text("Role 1")) {
    ForEach(roleList) { role in
        Text("\(role.roles)").tag(role.index)
    }
                                                            
}
© www.soinside.com 2019 - 2024. All rights reserved.