-1
{
        QueueId = 27;
        SongId = 38;
        artWorkURL = "<null>";
        duration = 58258;
        "stream_url" = "https://api.soundcloud.com/tracks/233301835/stream";
        title = Magenta;
        trackID = 233301835;
        userAvatar = "https://i1.sndcdn.com/avatars-000188204071-llusgk-large.jpg";
        userName = Agiv;
    },
            {
        QueueId = 27;
        SongId = 39;
        artWorkURL = "<null>";
        duration = 79000;
        "stream_url" = "https://api.soundcloud.com/tracks/233301833/stream";
        title = Nino;
        trackID = 233301833;
        userAvatar = "https://i1.sndcdn.com/avatars-000157591669-eva3mg-large.jpg";
        userName = "SWR Umwelt und Ern\U00e4hrung";
    }

My array of dictionary format is as above and multiple tracks i want to check 27 is already there or not ?

Janak LN
  • 138
  • 2
  • 15
  • http://stackoverflow.com/questions/31315232/check-if-value-is-in-the-array-as-dictionary-value already checked this not working – Janak LN Nov 16 '15 at 13:41
  • 1
    You can iterate over whole array and parse each dictionary individually to check for `trackID` 25. – NSNoob Nov 16 '15 at 13:43
  • is this an array or dictionary?. As far as i know you cannot add key-value pairs to an array. How is this format even possible? – Pratyusha Terli Nov 16 '15 at 14:13
  • @PratyushaTerli it is an array of dictionaries. i.e. an array which comprises of members of type dictionary. – NSNoob Nov 16 '15 at 14:30
  • I is edited now @NSNoob it was "track":["abc":"def","test":"test2"] in this format earlier – Pratyusha Terli Nov 16 '15 at 14:33

3 Answers3

3

You can do this with the filter function

let queueID27Exists = !array.filter({$0["QueueId"] as? Int == 27}).isEmpty
vadian
  • 274,689
  • 30
  • 353
  • 361
1

This answer is for your previous JSON object!

if let results : NSDictionary = post.objectForKey("data") as? NSDictionary {
     let array:NSArray = (results.valueForKey("track") as! NSArray)
     if "25" == array[0].valueForKey("trackID") as? String {
         NSLog("YES")
     } else {
         NSLog("NO")
     }
 }
NSNoob
  • 5,548
  • 6
  • 41
  • 54
Ebru Güngör
  • 336
  • 6
  • 14
0
var found = false    
for (_, data) in post {
    let track = data["track"]
    if track["trackID"] == "25" {
        found = true
    }
Tim
  • 2,089
  • 1
  • 12
  • 21