简体   繁体   中英

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

{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"

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.

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

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