gpt4 book ai didi

ios - 如何从 Facebook Graph API GraphResponse 协议(protocol)访问数据

转载 作者:可可西里 更新时间:2023-11-01 01:08:35 26 4
gpt4 key购买 nike

我是 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
}

我需要从这个 block 访问“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 , emailid

    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)")
}
}

关于ios - 如何从 Facebook Graph API GraphResponse 协议(protocol)访问数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51500623/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com