Could not cast value of type 'NSMutableURLRequest' (0x11beb7040) to 'Alamofire.URLRequestConvertible' (0x11beb9040)

后端 未结 3 1199
别那么骄傲
别那么骄傲 2021-01-03 04:47

when build my code then crash on Alamofire.request(request as! URLRequestConvertible).responseJSON(). pls suggest how to fix this crash

let url         


        
相关标签:
3条回答
  • 2021-01-03 05:11

    change let request = NSMutableURLRequest(url:url! as URL) to var request = URLRequest(url: NSURL(string: url)! as URL) your code look like this:-

        let url = NSURL(string: "https://jsonplaceholder.typicode.com/users")
        var request = URLRequest(url: NSURL(string: url)! as URL)
        request.httpMethod = "GET"
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    
        Alamofire.request(request as! URLRequestConvertible).responseJSON(){ response in
    
               switch response.result
               {
                  case .success(let data):                
                     print(" i got my Data Yup",data)          
                  case .failure(let error):
                     print(error)                                
                }
            }
    
    0 讨论(0)
  • 2021-01-03 05:13

    You can also try with following syntax for request method:

    Alamofire.request(strURL,method: .post, parameters: parameters as? [String : AnyObject], encoding: URLEncoding.default)
    
    0 讨论(0)
  • 2021-01-03 05:22

    In Swift 3, all you have to do is to change NSMutableURLRequest to URLRequest and it will work. No need to cast. Also, set var so it can be swift mutable. :) Thanks, Markus for comment.

    0 讨论(0)
提交回复
热议问题