第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

AlamoFire GET api請求無法按預期工作

AlamoFire GET api請求無法按預期工作

慕慕森 2019-09-13 12:10:18
AlamoFire GET api請求無法按預期工作我正在努力學習如何使用AlamoFire而我遇到了麻煩。到目前為止,我的方法如下:func siteInfo()->String?{     var info:NSDictionary!     var str:String!     Alamofire.request(.GET, MY_API_END_POINT).responseJSON {(request, response, JSON, error) in         info = JSON as NSDictionary         str = info["access_key"] as String         //return str    }     return str}這返回nil這是一個問題。從我在這里讀到的,這是因為請求可能需要一段時間,因此在返回之前關閉不會執(zhí)行。將返回值移動到閉包中的建議解決方案對我來說不起作用,編譯器只是大喊(->String在之后添加(request,response,JSON,error)“'String'不是void的子類型”)。所提供的其他解決方案也是如此。有任何想法嗎?甚至一些與此問題無關的源代碼(使用AlamoFire)也會有所幫助。謝謝!
查看完整描述

1 回答

?
呼如林

TA貢獻1798條經(jīng)驗 獲得超3個贊

處理這個的一種方法是將一個閉包(我通常稱之為a completionHandler)傳遞給你的siteInfo函數(shù)并調(diào)用它的內(nèi)部Alamofire.request閉包:

func siteInfo(completionHandler: (String?, NSError?) -> ()) -> () {
    Alamofire.request(.GET, MY_API_END_POINT).responseJSON {
        (request, response, JSON, error) in

        let info = JSON as? NSDictionary // info will be nil if it's not an NSDictionary        let str = info?["access_key"] as? String // str will be nil if info is nil or the value for "access_key" is not a String
        completionHandler(str, error)
    }}

然后像這樣調(diào)用它(不要忘記錯誤處理):

siteInfo { (str, error) in
    if str != nil {
        // Use str value    } else {
        // Handle error / nil value    }}

在評論中你問:

那么如果你只能在閉包內(nèi)部做東西而不影響閉包之外的對象,你將如何保存從get請求中收集的信息?另外,如何跟蹤知道請求何時完成?

您可以從閉包內(nèi)部將get請求的結(jié)果保存到類中的實例變量中; 封閉阻止你做這件事沒有任何關系。你從那里做什么真的取決于你想用這些數(shù)據(jù)做什么。

一個例子怎么樣?

由于看起來您正在獲取獲取請求的訪問密鑰表單,因此您可能需要將其用于將來在其他功能中發(fā)出的請求。

在這種情況下,你可以這樣做:

注意:異步編程是一個很大的主題; 太多了,無法覆蓋這里。這只是您如何處理從異步請求中獲取的數(shù)據(jù)的一個示例。

public class Site {
    private var _accessKey: String?

    private func getAccessKey(completionHandler: (String?, NSError?) -> ()) -> () {

        // If we already have an access key, call the completion handler with it immediately        if let accessKey = self._accessKey {
            completionHandler(accessKey, nil)
        } else { // Otherwise request one            Alamofire.request(.GET, MY_API_END_POINT).responseJSON {
                (request, response, JSON, error) in

                let info = JSON as? NSDictionary // info will be nil if it's not an NSDictionary                let accessKey = info?["access_key"] as? String // accessKey will be nil if info is nil or the value for "access_key" is not a String
                self._accessKey = accessKey
                completionHandler(accessKey, error)
            }
        }
    }

    public func somethingNeedingAccessKey() {
        getAccessKey { (accessKey, error) in
            if accessKey != nil {
                // Use accessKey however you'd like here                println(accessKey)
            } else {
                // Handle error / nil accessKey here            }
        }
    }}

使用該設置,somethingNeedingAccessKey()第一次調(diào)用將觸發(fā)獲取訪問密鑰的請求。之后的任何調(diào)用somethingNeedingAccessKey()都將使用已存儲的值self._accessKey。如果你somethingNeedingAccessKey在傳遞給閉包的其余部分工作getAccessKey,你可以確定你的accessKey總是有效的。如果您需要另一個需要的功能accessKey,只需按照相同的方式somethingNeedingAccessKey編寫即可。

public func somethingElse() {
    getAccessKey { (accessKey, error) in
        if accessKey != nil {
            // Do something else with accessKey        } else {
            // Handle nil accessKey / error here        }
    }}




查看完整回答
反對 回復 2019-09-16
  • 1 回答
  • 0 關注
  • 567 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號