【发布时间】:2016-11-07 10:25:11
【问题描述】:
我正在使用 Swift 3.0 开发一个项目,并且我有一个使用 Facebook 的身份验证注册程序。所以基本上我已经完成了 FB 集成部分,并且一旦登录到一个帐户,我就得到了用户的电子邮件地址、姓名、ID 和个人资料图片的 url,因为我已经请求了他们(这就是我进入我的控制台)。我收到的格式如下。
Optional({
email = "xxxxx@yahoo.com";
id = xxxxxxxxxx;
name = "xxxxxx xxxx";
picture = {
data = {
"is_silhouette" = 0;
url = "https://scontent.xx.fbcdn.net/v/t1-1/c2.0.50.50/p50x50/10441341_10201209786847369_5426097469926927988_n.jpg?oh=1b4d8709be17338bc7e877d411b96eee&oe=58CE5B13";
};
};
})
我的要求是将这些电子邮件、姓名、ID 字段分配给标签并打印出来。
我的相关视图控制器的代码如下。我应该添加什么??
import UIKit
import FBSDKLoginKit
class ViewController: UIViewController,FBSDKLoginButtonDelegate{
@IBOutlet weak var displayLabel: UILabel!
@IBOutlet weak var loginButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
let loginButton = FBSDKLoginButton()
view.addSubview(loginButton)
//frame's are obselete, please use constraints instead because its 2016 after all
loginButton.frame = CGRect(x: 16, y: 50, width: view.frame.width - 32, height: 50)
loginButton.delegate = self
loginButton.readPermissions = ["email", "public_profile"]
}
func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
print("Did log out of facebook")
}
@IBAction func loginButtonPressed(_ sender: AnyObject) {
FBSDKLoginManager().logIn(withReadPermissions: ["email"], from: self) { (result, err) in
if err != nil {
print("Custom FB Login failed:", err)
return
}
print("FB login Sucess")
print(result?.token.tokenString)
self.showEmailAddress()
}
}
func showEmailAddress() {
FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email,picture"]).start { (connection, result, err) in
if err != nil {
print("Failed to start graph request:", err)
return
}
print(result)
// self.displayLabel.text = result["email"]as? NSMutableArray
}
}
}
【问题讨论】:
-
result["email"]as? NSMutableArray为什么是 NSMutableArray?as String,不是吗? -
尽管你放了什么,但它给出了一条错误消息,说“Type Any 没有订阅成员”。这就是为什么我评论了那条线先生。只是想将该结果分配给标签,因为我已经尝试过但失败了
-
你必须告诉编译器,
result是一个字典(在我们的例子中:[String:Any])。
标签: ios swift dictionary swift3 facebook-sdk-4.x