Swift 2.0 - 图形API - 如何存储接收的数据?

时间:2016-02-28 05:30:42

标签: ios swift facebook-graph-api

我正在编写一个使用Graph API的函数,用于搜索地点,

FBSDKGraphRequest(graphPath: "/search", parameters: ["q":"coffee",
                        "type":"place", "fields":"id,name", "HTTPMethod":"GET"])
                    .startWithCompletionHandler({ (connection, result, error) -> Void in

这是我的搜索结果的屏幕截图:

enter image description here

我正在尝试将这些信息提取到我的应用中...到目前为止,我发现这些信息存储在结果[“数据”] 中,但我不知道如何更深入并得到这些id和名称..

如果您有任何想法,请分享

2 个答案:

答案 0 :(得分:0)

您可以将结果[" data"]存储为数组,之后您可以检查包含值的字典的特定索引

例如:

if let arrData = result["data"] as? [Dictionary<String,AnyObject>] {
  let name = arData[0]["name"]
  print("name \(name)")
}

如果您要在tableview / collection视图中显示它,就像将数组传递给行并在单个索引路径获取字典

答案 1 :(得分:0)

首先创建一个空字典

var receivedIdAndNames = [Int:String]()

FBSDKGraphRequest(graphPath: "/search", parameters: ["q":"coffee",
                    "type":"place", "fields":"id,name", "HTTPMethod":"GET"])
                .startWithCompletionHandler({ (connection, result, error) -> Void in

,比图完成处理程序;

for (id,name) in result[data] as! Dictionary {

   receivedIdAndNames[id] = name } //end of for loop

最后,您可以在所需代码中的任何位置使用receivedIdAndNames。