简体   繁体   English

如何检查NsmutableArray中是否包含字符串值

[英]how to check contains string value in NsmutableArray

i getting some response which contains some information, i stored value in dictionary format in NSMutableArray for example 我得到一些包含一些信息的响应,例如,我以字典格式将值存储在NSMutableArray中

{name : daniel, email : xxx.com},{name: daniel, email : yay.com},{name : daniel, email : zzz.com} {name: Annie, email : annie.com},{name: George, email : ger.com}

i have two label Name and Email. 我有两个标签名称和电子邮件。 when i press Label Action i displayed all the usernames in that i selecting name as "daniel" 当我按“标签操作”时,我在其中显示所有用户名,并选择名称为“ daniel”

Now i going for second label called Email Action in that i want only email list of "daniel" its contains three mail i have to displayed in tableview 现在,我要使用第二个标签,称为“电子邮件操作”,因为我只希望“丹尼尔”的电子邮件列表包含三个必须在表格视图中显示的邮件

here i tried this code but didn't worked for me 在这里我尝试了这段代码,但没有为我工作

if NameLabel.text!.containsString(self.NameList.valueForKey("Name") as! String){
             NSLog("success")
        }else{
            NSLog("fail")
        }

my output log : 我的输出日志:

Could not cast value of type '__NSArrayI' (0x1059da8b0) to 'NSString' (0x103a50b20).

try this 尝试这个

let Namelist = [["name" : "daniel", "email" : "abc@gmail.com"]]

let data = Namelist.filter{($0 as [String:String])["name"]!.lowercaseString.containsString("daniel".lowercaseString)}.map { x in return x["email"]!}
print(data)

I have tested it in playground 我在操场上测试过

在此处输入图片说明

You have a dictionary array that means that you need to loop through the array and then check if each dictionary contains a value for the key 'name' that is equal to your nameLabel text and then get the email from the same dictionary. 您有一个字典数组,这意味着您需要遍历该数组,然后检查每个字典是否包含与您的nameLabel文本相等的键“ name”的值,然后从同一字典中获取电子邮件。

let nameList : NSMutableArray = [["name":"daniel","email":"daniel@test"], ["name":"martin","email":"martin@test"], ["name":"john","email":"john@test"]]

...
let nameLabel = UILabel()
nameLabel.text = "daniel"
...

for list in nameList {

    if let dic = list as? [String: String] {


        if dic["name"] == nameLabel.text {
            let email = dic["email"]
            print("\(email)")
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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