2017-07-06 38 views
0

我試圖發送從facebookSDK收集的用戶信息,並執行HTTP POST,發送我收集的存儲在常量中的詳細信息。從另一個函數引用常量

我是swift開發新手,雖然這個解決方案可能很簡單,但我無法配置一個可行的解決方案。

我需要將fetchProfile()函數中的數據傳遞給data_request()函數以將數據傳遞給API。

The image shows the data_request function, highlighted is the line where i need to reference the data stored in constants within the fetchProfile() function

This image shows the fetchProfile() function holding the data needed to be passed to data_request()

任何反饋或指導不勝感激。

+0

首先,請去閱讀[問]。相關代碼以文本形式直接屬於您的問題,而不屬於外部網站上的圖片。 – CBroe

回答

1

您的函數都有閉包,這意味着從異步網絡調用回撥。所以你的主要功能將首先完成,然後當你的網絡通話結束時,關閉被調用。所以在你的情況下,你需要等到fetchProfile()完成異步調用才能獲取數據,然後在data_request()函數中使用它。所以它看起來像

func fetchProfile(){ 
    //... 
    FBSDKGraphRequest.... 
     void in 
     //prepare your data here 
     //call data_request 
} 
0

提高後發表放鳴的答案,你需要()在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 
} 
相關問題