ios - 如何从 Facebook Graph API 的 GraphResponseProtocol 访问数据
问题描述
我对 swift 很陌生,我现在正在研究 Facebook graph api。我无法访问来自图形请求的数据。
struct MyProfileRequest: GraphRequestProtocol {
struct Response: GraphResponseProtocol {
init(rawResponse: Any?) {
// Decode JSON from rawResponse into other properties here.
let json = JSON(rawResponse!)
let userDef : [String : String] = [
"username" : json["name"].stringValue,
"lastname" : json["last_name"].stringValue,
"shortname" : json["short_name"].stringValue,
"name_format" : json["name_format"].stringValue
]
}
}
var graphPath = "/me"
var parameters: [String : Any]? = [ "fields": "id, name, last_name, name_format, short_name"]
var accessToken = AccessToken.current
var httpMethod: GraphRequestHTTPMethod = .GET
var apiVersion: GraphAPIVersion = .defaultVersion
}
我需要访问此块之外的“userDef”字典。
解决方案
这很简单只需检查此示例代码
struct MyProfileRequest: GraphRequestProtocol {
struct Response: GraphResponseProtocol {
var name:String?
var email:String?
var id:String?
init(rawResponse: Any?) {
print(rawResponse)
// Decode JSON from rawResponse into other properties here.
if let response = rawResponse as? [String:Any] {
name = (response["name"] as? String) ?? ""
email = (response["email"] as? String) ?? ""
id = (response["id"] as? String) ?? ""
}
}
}
var graphPath = "/me"
var parameters: [String : Any]? = ["fields": "id, name,email"]
var accessToken = AccessToken.current
var httpMethod: GraphRequestHTTPMethod = .GET
var apiVersion: GraphAPIVersion = .defaultVersion
}
如何访问 name
,email
以及id
MyProfileRequest().start {[unowned self] (req, result) in
switch result {
case .success(let values): // Here is your values
print("Custom Graph Request Succeeded: \(values)")
print(values.name)
case .failed(let error):
print("Custom Graph Request Failed: \(error)")
}
}
推荐阅读
- php - 只有三星手机没有收到来自 fcm 的通知
- javascript - 如何使 Bootstrap 日期和时间选择器在顶部打开而不是打开
- ionic4 - 我们可以在移动设备和台式机上使用signle ionic-4 应用吗
- github - 如何安装宝石?
- spring - spring boot restTempalte call (get Data) with security handel
- javascript - 根据滚动位置更改 CSS 参数
- javascript - 从 HTML 节点获取没有文本的 html 结构
- ios - 如何在具有 dequeueReusableCell 的同时以编程方式设置 tableview 样式以具有字幕?
- angular - 如何计算与角度条件匹配的项目?
- java - 如何使用 java + selenium 设置 chrome 浏览器语言?