简体   繁体   中英

Check if value is present in JSON array, if not check next array(Swift / SwiftUI)

I have a username and a password, I need to check both of these against the data in the JSON file, if both are correct/exist I return a list that contains the data in "items".

For example: if I type "user" for the username and "pass" for the password the view should display "Dog, Cat, Mouse, Parrot, Goldfish" in a list.

The JSON file can be modified if my syntax is incorrect.

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 Code:

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

You can use first(where:) to find an item in your array that matches a given condition (in this case, that the user names match). It returns an optional since there's no guarantee that there will be an item that matches.

I had to stub out UserModel and UserModelData since you didn't include them, so you'll need to make sure the type names match what you had.

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

You can achieve this using filter:

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

Additionally you could move this logic to the UserModelData and do something like:

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

Where:

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM