【发布时间】:2021-06-03 17:36:25
【问题描述】:
我有用户名和密码,我需要根据 JSON 文件中的数据检查这两项,如果两者都正确/存在,我会返回一个包含“项目”中数据的列表。
例如:如果我输入“user”作为用户名并输入“pass”作为密码,视图应该在列表中显示“Dog, Cat, Mouse, Parrot, Goldfish”。
如果我的语法不正确,可以修改 JSON 文件。
JSON:
[
{
"username": "user",
"password": "pass",
"type": "Animals",
"items": ["Dog",
"Cat",
"Mouse",
"Parrot",
"Goldfish"
]
},
{
"username": "helloworld",
"password": "firstprogram",
"type": "States",
"items": ["Not Running",
"Inactive",
"Active",
"Background",
"Suspended"
]
},
{
"username": "movielover",
"password": "bestmovies",
"type": "Movies",
"items": ["Kill Bill",
"Us",
"Parasite",
"Coco",
"Inception"
]
},
]
Swift UI 代码:
import SwiftUI
struct LoginView: View {
var userName = "user"
var items: UserModel = UserModelData().userInformation[0]//this only displays data
for the first item in the array, I would like to check each item,find the one
that contains userName, and use that index.
var body: some View{
VStack{
Text("\(items.username)'s \(items.type)")
.font(.title)
.fontWeight(.bold)
if userName == items.username{
Form{
List(items.items, id: \.self){ item in
Text(item)
}
}
}
}
}
}
struct LoginView_Previews: PreviewProvider {
static var previews: some View {
LoginView()
}
}
【问题讨论】: