如何在数组中找到并获得价值

时间:2016-11-16 14:57:03

标签: ios swift find swift3

let firstArray = 
[[name: "John", id: "001", active: "true"],
[name: "Paul", id: "002", active: "false"]]

let user: User? //This user contains information like an id (002)

你好,我想告诉"查看每个profil中的firstArray,并找到id相同的profil。在示例中,它是保罗。我想找回paul profil [姓名:" Paul",id:" 002",活跃:" false"]

我希望现在更加干净,感谢您的反馈

2 个答案:

答案 0 :(得分:1)

您应该为您的用户使用课程,例如:

class user{
    var user_id:String
    var name:String

    init(user_id:String, name:String){
        self.user_id=user_id
        self.name=name
    }
}

然后,创建一个数组来存储它们:

let user_array=[user1, user2]

当您需要比较用户时:

let user1=user_array[0]
if user1.id==some_id{
    //then...
}

答案 1 :(得分:1)

for item in firstArray{
    let user_id=item.value(forKey: "id") as! String
    if user_id==some_id{
        print("found user")
    }
}