【问题标题】:Check if value is present in JSON array, if not check next array(Swift / SwiftUI)检查 JSON 数组中是否存在值,如果不存在则检查下一个数组(Swift / SwiftUI)
【发布时间】: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()
    }
}

【问题讨论】:

    标签: json swift swiftui


    【解决方案1】:

    您可以使用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)
                        }
                    }
                }
                
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用过滤器实现此目的:

      var item: UserModel = UserModelData().userInformation.filter({ $0.username == "user" && $0.password == "pass" }).first
      

      此外,您可以将此逻辑移至 UserModelData 并执行以下操作:

      var serModelData = UserModelData()
      lazy var item: UserModel = userModelData.retrieveUserModel(for: "user", and: "pass")
      

      地点:

      class UserModelData {
          ...
          func retrieveUserModel(for username: String, and password: String) {
              userInformation
                  .filter({ $0.username == username && $0.password == password }).first
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多