swift - 如何从 FBSDKGraphRequest 结果将用户名设置为 UITextField
问题描述
我正在使用 Xcode 10、Swift 5,并且试图将 UITextField 设置为我从 Facebook 检索到的用户名。我已成功检索到结果中的 ID、电子邮件和名称,但是当我将结果发送到文本字段时,包含所有三个字段。我只想要名字。
func getUserProfile() {
let req = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"name"], tokenString: accessToken?.tokenString, version: nil, httpMethod: "GET")
req?.start(completionHandler: { (connection, result, error : Error!) -> Void in
if(error == nil)
{
print("\(String(describing: result))")
self.FBUserName.text = "name \(String(describing: result))"
}
else
{
print("error \(String(describing: error))")
}
})
}
解决方案
您可以将结果转换为[String : Any]
,像这样
if error != nil {
print("Error: \(error!.localizedDescription)")
} else if let result = result as? [String : Any] {
self.FBUserName.text = result["name"] as! String
}
这是我的工作要求
let graphRequest: FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "email,name"])
graphRequest.start(completionHandler: { [weak self] (connection, result, error) -> Void in
if error != nil {
print("Error: \(error!.localizedDescription)")
} else if let result = result as? [String : Any], let strongSelf = self {
strongSelf.txtName.text = (result["name"] as! String)
strongSelf.txtEmail.text = (result["email"] as! String)
}
})
推荐阅读
- amazon-web-services - 使用 terraform 的 AWS Autoscaling 配置出错
- html - 为什么删除边框厚度和颜色保持边框宽度会删除边框本身?
- javascript - 如何定位动态生成的元素?
- ubuntu - 如何将 Fedora 中的依赖项转换为 Ubuntu
- windows-machine-learning - Tensorfloat 和 ImageFeatureValue 的区别
- python - python中模拟Delphi-7 MOVE函数
- c# - 如何通过 Rest 请求调用打开 URL
- css - CSS - 动态删除字体下降器
- asp.net-core - .NET 6 预览版 4:没有可用于指定 RuntimeIdentifier 'browser-wasm' 的 Microsoft.AspNetCore.App 运行时包
- google-apps-script - 使用谷歌应用脚本从谷歌文档中获取页数