首页 > 解决方案 > 检查 JSON 数组中是否存在值,如果不存在则检查下一个数组(Swift / SwiftUI)

问题描述

我有用户名和密码,我需要根据 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"
        ]
    },
]

斯威夫特用户界面代码:

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()
    }
}

标签: jsonswiftswiftui

解决方案


您可以使用first(where:)在数组中查找与给定条件匹配的项目(在这种情况下,用户名匹配)。它返回一个可选的,因为不能保证会有一个匹配的项目。

我不得不存根UserModelUserModelData因为你没有包括它们,所以你需要确保类型名称与你所拥有的匹配。

struct UserModel {
    var username : String
    var password : String
    var items : [String]
    var type: String
}

struct UserModelData {
    var userInformation : [UserModel] = []
}

struct LoginView: View {
    
    var userName = "user"
    
    private var userModelData = UserModelData()
    
    var userInformation : UserModel? {
        userModelData.userInformation.first { $0.username == userName }
    }
    
    var body: some View{
        VStack {
            
            if let userInformation = userInformation {
                Text("\(userInformation.username)'s \(userInformation.type)")
                    .font(.title)
                    .fontWeight(.bold)
                
                Form {
                    List(userInformation.items, id: \.self) { item in
                        Text(item)
                    }
                }
            }
            
        }
    }
}

推荐阅读