Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift (AFNetworking)如何调整AFHTTPRequestOperation请求中的标头?_Swift_Afnetworking_Afhttprequestoperation - Fatal编程技术网

Swift (AFNetworking)如何调整AFHTTPRequestOperation请求中的标头?

Swift (AFNetworking)如何调整AFHTTPRequestOperation请求中的标头?,swift,afnetworking,afhttprequestoperation,Swift,Afnetworking,Afhttprequestoperation,创建AFHTTPRequestOperation标题后如何修改它?在API请求返回401的情况下,我需要刷新访问令牌并调整每个AFHTTPRequestOperation的头,然后使用更新的访问令牌重试相同的请求操作 下面是我处理由于当前正在刷新访问令牌而排队的请求的代码。当接收到新的访问令牌时,将调用此方法 下面的方法是可行的,但创建操作请求的新变量并调整变量的头也会更改原始请求,这似乎很奇怪 var authManager = AFOAuth2Manager() func processH

创建
AFHTTPRequestOperation
标题后如何修改它?在API请求返回
401
的情况下,我需要刷新访问令牌并调整每个
AFHTTPRequestOperation
的头,然后使用更新的访问令牌重试相同的请求操作

下面是我处理由于当前正在刷新访问令牌而排队的请求的代码。当接收到新的访问令牌时,将调用此方法

下面的方法是可行的,但创建操作请求的新变量并调整变量的头也会更改原始请求,这似乎很奇怪

var authManager = AFOAuth2Manager()

func processHeldRequests() {
        for operation: AFHTTPRequestOperation in heldRequests {
            var token = tokenManager.getToken()
            println("adjusting held operation's request header authorization to new token \(token!)")

            var operationRequest: NSMutableURLRequest = operation.request as! NSMutableURLRequest
            operationRequest.setValue("Bearer \(token!)", forHTTPHeaderField: "Authorization")
            authManager.operationQueue.addOperation(operation)
        }
        heldRequests.removeAll(keepCapacity: false)
    }