从另一个函数引用常量
问题描述:
我试图发送从facebookSDK收集的用户信息,并执行HTTP POST,发送我收集的存储在常量中的详细信息。从另一个函数引用常量
我是swift开发新手,虽然这个解决方案可能很简单,但我无法配置一个可行的解决方案。
我需要将fetchProfile()函数中的数据传递给data_request()函数以将数据传递给API。
This image shows the fetchProfile() function holding the data needed to be passed to data_request()
任何反馈或指导不胜感激。
答
您的函数都有闭包,这意味着从异步网络调用回拨。所以你的主要功能将首先完成,然后当你的网络通话结束时,关闭被调用。所以在你的情况下,你需要等到fetchProfile()
完成异步调用才能获取数据,然后在data_request()
函数中使用它。所以它看起来像
func fetchProfile(){
//...
FBSDKGraphRequest....
void in
//prepare your data here
//call data_request
}
答
提高后发表放鸣的答案,你需要()在fetchProfile接收到的数据传送到data_request(),并在FBSDKGraphRequest.start
结束块结束调用它。这是应该的样子:
func fetchProfile() {
FBSDKGraphRequest(...).start {
// create a dictionary to hold all the values returned
let values: [String: Any]
// fetch all values in to the dictionary
// ...
// call data_request by passing URL and the dictionary
data_request(url, params: values)
}
}
现在data_request
功能应该是现在这个样子:
func data_request(url: String, params: [String: Any]) {
// your implementation here
}
首先,请去阅读[问]。相关代码以文本形式直接属于您的问题,而不属于外部网站上的图片。 – CBroe