如何在字典数组中检查值是否为值?

时间:2015-11-16 13:38:24

标签: ios swift swift2

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

我的字典格式数组如上所示,我要检查27的多个曲目是否已经存在?

3 个答案:

答案 0 :(得分:2)

您可以使用filter功能

执行此操作
let queueID27Exists = !array.filter({$0["QueueId"] as? Int == 27}).isEmpty

答案 1 :(得分:1)

这个答案适用于以前的JSON对象!

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

答案 2 :(得分:0)

var found = false    
for (_, data) in post {
    let track = data["track"]
    if track["trackID"] == "25" {
        found = true
    }