Facebook graph api 使用Swift在iOS SDK中处理Facebook图形API结果
我只想从Facebook的Graph API请求数据,例如获取当前用户的基本信息 目标-C文档是:Facebook graph api 使用Swift在iOS SDK中处理Facebook图形API结果,facebook-graph-api,swift,facebook-ios-sdk,Facebook Graph Api,Swift,Facebook Ios Sdk,我只想从Facebook的Graph API请求数据,例如获取当前用户的基本信息 目标-C文档是: 目前还没有Swift文档,我对类型为“id”的“result”参数感到困惑。它看起来像result包含一个字典,但它可能是nil。在Swift中,其类型将映射到AnyObject? 因此,在Swift中,您可以执行以下操作: // Cast result to optional dictionary type let resultdict = result as? NSDictionary if
目前还没有Swift文档,我对类型为“id”的“result”参数感到困惑。它看起来像
result
包含一个字典,但它可能是nil
。在Swift中,其类型将映射到AnyObject?
因此,在Swift
中,您可以执行以下操作:
// Cast result to optional dictionary type
let resultdict = result as? NSDictionary
if resultdict != nil {
// Extract a value from the dictionary
let idval = resultdict!["id"] as? String
if idval != nil {
println("the id is \(idval!)")
}
}
这可以简化一点:
let resultdict = result as? NSDictionary
if let idvalue = resultdict?["id"] as? String {
println("the id value is \(idvalue)")
}
请记住,它不是一本字典,而是字典和数组的组合
FBRequestConnection.startWithGraphPath("me?fields=feed", completionHandler: { (connection, result, error) -> Void in
if( error == nil){
let fbGraphObject = result as FBGraphObject
let feed = fbGraphObject.objectForKey("feed") as NSMutableDictionary
let data = feed.objectForKey("data") as NSMutableArray
let postDescription = data[0].objectForKey("description") as String
//println( post )
self.fbu.initialUserFeed = feed
self.performSegueWithIdentifier("SelectStreams", sender: self)
}else
{
//TODO Allert to user that something went wrong
println(error)
}
})
一开始我对此感到困惑这是一种更简单的方法:
let params: [NSObject : AnyObject] = ["redirect": false, "height": 800, "width": 800, "type": "large"]
let pictureRequest = FBSDKGraphRequest(graphPath: "me/picture", parameters: params, HTTPMethod: "GET")
pictureRequest.startWithCompletionHandler({
(connection, result, error: NSError!) -> Void in
if error == nil {
print("\(result)")
let dictionary = result as? NSDictionary
let data = dictionary?.objectForKey("data")
let urlPic = (data?.objectForKey("url"))! as! String
print(urlPic)
} else {
print("\(error)")
}
})
}
成功了,谢谢!我认为这是目前唯一一个第一个iOS语言为Swift.fbGraphObject的用户在线FB SDK文档
let params: [NSObject : AnyObject] = ["redirect": false, "height": 800, "width": 800, "type": "large"]
let pictureRequest = FBSDKGraphRequest(graphPath: "me/picture", parameters: params, HTTPMethod: "GET")
pictureRequest.startWithCompletionHandler({
(connection, result, error: NSError!) -> Void in
if error == nil {
print("\(result)")
let dictionary = result as? NSDictionary
let data = dictionary?.objectForKey("data")
let urlPic = (data?.objectForKey("url"))! as! String
print(urlPic)
} else {
print("\(error)")
}
})
}