1

我对 iOS Facebook SDK 很陌生,而且速度很快,并且遇到了一个小问题。

我想从 Facebook 用户那里获取个人资料信息,例如姓名、生日和音乐。

我尝试关注姓名和生日,但不知道如何遍历 NSDictionary。最后,我想将所有艺术家/乐队名称保存在名为 allMusic 的字符串变量中

let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me?fields=id,name,email,bio,birthday,hometown,gender,music,events", parameters: nil)
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in

if ((error) != nil)
{
// Process error
print("Error: \(error)")
}
else
{
let userName : NSString = result.valueForKey("name") as! NSString
let userEmail : NSString = result.valueForKey("email") as! NSString
let bday : NSString = result.valueForKey("birthday") as! NSString
let bio : NSString = result.valueForKey("bio") as! NSString
let id : NSString = result.valueForKey("id") as! NSString
let music: NSDictionary = result.valueForKey("music") as! NSDictionary
    var allMusic: String = ""
    for val in music {
        print(val.valueForKey("data"))
         //        allMusic += "----Value---"
    }
}
})

如果我打印(音乐),这会出现在命令行上

{
data =     (
            {
        "created_time" = "2015-07-19T12:17:26+0000";
        id = 345946458834143;
        name = Wanda;
    },
            {
        "created_time" = "2015-06-23T01:20:04+0000";
        id = 119490691442848;
        name = Knorkator;
    }... and so on
            {
        "created_time" = "2013-02-11T19:13:08+0000";
        id = 8811047260;
        name = "Infected Mushroom";
    }
);
paging =     {
    cursors =         {
        after = ODgxMTA0NzI2MAZDZD;
        before = MzQ1OTQ2NDU4ODM0MTQz;
    };
    next = "some url...";
};
}
4

1 回答 1

1
let array = ((result as! NSDictionary).value(forKey: "data") as! NSArray)

                for myData in array{
                    let music = ((myData as! NSDictionary).value(forKey: "name")) as! String //you can also save this to global variable :)

                }

希望这会工作的朋友

于 2017-03-21T12:29:44.863 回答