Facebook API 正在检索 swift 中的好友列表
Facebook API retrieving friends list in swift
为什么这会给我一个错误?
fatal error: unexpectedly found nil while unwrapping an Optional value
我是不是用错了valueForKey
String
?我想获取好友列表。 (有朋友因为当我打印结果时它显示了他们)
let fbRequest = FBSDKGraphRequest(graphPath:"/me/friends", parameters: nil);
fbRequest.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in
if error == nil {
let userName : NSArray = result.valueForKey("name") as! NSArray
print("Friends are : \(result)")
} else {
print("Error Getting Friends \(error)");
}
}
试试这个
let fbRequest = FBSDKGraphRequest(graphPath:"/me/friends", parameters: nil);
fbRequest.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in
if error == nil {
if let userNameArray : NSArray = result.valueForKey("data") as! NSArray
{
var i:Int = 0
for i;i<userNameArray.count ; i++
{
print(userNameArray[i].valueForKey("name"))
}
} else {
print("Error Getting Friends \(error)");
}
}
为什么这会给我一个错误?
fatal error: unexpectedly found nil while unwrapping an Optional value
我是不是用错了valueForKey
String
?我想获取好友列表。 (有朋友因为当我打印结果时它显示了他们)
let fbRequest = FBSDKGraphRequest(graphPath:"/me/friends", parameters: nil);
fbRequest.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in
if error == nil {
let userName : NSArray = result.valueForKey("name") as! NSArray
print("Friends are : \(result)")
} else {
print("Error Getting Friends \(error)");
}
}
试试这个
let fbRequest = FBSDKGraphRequest(graphPath:"/me/friends", parameters: nil);
fbRequest.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in
if error == nil {
if let userNameArray : NSArray = result.valueForKey("data") as! NSArray
{
var i:Int = 0
for i;i<userNameArray.count ; i++
{
print(userNameArray[i].valueForKey("name"))
}
} else {
print("Error Getting Friends \(error)");
}
}