JSONResponseSerializer 和 HTTPURLResponse Swift 3.0 的 Alamofire4 问题

Posted

技术标签:

【中文标题】JSONResponseSerializer 和 HTTPURLResponse Swift 3.0 的 Alamofire4 问题【英文标题】:Alamofire4 trouble with JSONResponseSerializer & HTTPURLResponse Swift 3.0 【发布时间】:2016-10-03 19:05:14 【问题描述】:

自从我更新 Alamofire 后,我收到错误消息:Type Request has no member JSONResponseSerializer 并且 cannot call value of non-function type HTTPURLResponse

我已将 Response 切换为 DataResponse,但仍然出现错误。

代码:

extension Alamofire.Request 
    func responseUserEventsArray(_ completionHandler: @escaping (DataResponse<UserEventsWrapper>) -> Void) -> Self 
        let responseSerializer = DataResponseSerializer<UserEventsWrapper>  request, response, data, error in
            guard error == nil else
            
                return .failure(error!)
            
            guard let responseData = data else 
                return .failure(AFError.responseSerializationFailed(reason: .inputDataNil))

            

            let JSONResponseSerializer = Request.JSONResponseSerializer(options: .allowFragments)
            let result = JSONResponseSerializer.serializeResponse(request, response, responseData, error)

            switch result 
            case .Success(let value):
                let json = JSON(value)
                let wrapper = UserEventsWrapper()

                wrapper.next = json["eventhistory"]["next_page_url"].stringValue
                wrapper.previous = json["eventhistory"]["prev_page_url"].stringValue
                wrapper.count = json["eventhistory"]["total"].intValue

                var allUserEvents:Array = Array<UserEvents>()
                print(json)
                let results = json["eventhistory"]["data"]
                print(results)

                for jsonAds in results
                
                    print(jsonAds.1)
                    let adsData = UserEvents(json: jsonAds.1, id: Int(jsonAds.0))
                    allUserEvents.append(adsData)
                
                wrapper.usereventsitems = allUserEvents
                return .success(wrapper)
            case .Failure(let error):
                return .Failure(error)
            
        

        return response(responseSerializer: responseSerializer,completionHandler: completionHandler)
    

【问题讨论】:

【参考方案1】:

已编辑


改变

Request.JSONResponseSerializerDataRequest.jsonResponseSerializer

extension Alamofire.Requestextension Alamofire.DataRequest – Mat0

.success.failure - FranMowinckel

【讨论】:

并将 Alamofire.Request 扩展为扩展 Alamofire.DataRequest 还有.success和.failure 感谢您再次帮助我!我会在它解锁时接受:)

以上是关于JSONResponseSerializer 和 HTTPURLResponse Swift 3.0 的 Alamofire4 问题的主要内容,如果未能解决你的问题,请参考以下文章

第三十一节:扫盲并发和并行同步和异步进程和线程阻塞和非阻塞响应和吞吐等

shell中$()和 ` `${}${!}${#}$[] 和$(()),[ ] 和(( ))和 [[ ]]

Java基础8---面向对象代码块和继承和this和super和重写和重载和final

Java基础8---面向对象代码块和继承和this和super和重写和重载和final

JS中some()和every()和join()和concat()和pop(),push(),shift(),unshfit()和map()和filter()

malloc和free,brk和sbrk和mmap和munmap的使用和关系以及内存分配的原理