Error: “Pattern cannot match values of type 'URLRequest'” in swift

后端 未结 2 865
盖世英雄少女心
盖世英雄少女心 2021-01-28 00:57

My code was working perfectly in before.. recently i have installed Alamofire in other project now below error throwing.

Pattern cannot ma

2条回答
  •  暖寄归人
    2021-01-28 01:05

    You can try the below code with latest alamofire.

      func addNewEvents()
        {
            let dict = ["eventName":"Abhi's Birthday", "eventMessage":"Please come and join us", "eventDate":"21-07-2020", "eventTime":"11:30AM", "eventEndDate":"21-07-2020", "eventEndTime":"11:00PM", "isAllDayEvent":"false", "isEventRepeatable":"false", "eventAddress":"123 Hyd Rd", "eventCity":"Secbad", "location":["latitude":"-23.345","longitude":"15.234"], "remindersList":["1-day","1-hours"], "eventFrequency":"Never", "numberOfOccurrences":"", "showGuests":true, "status":"Draft", "createGroup":"true", "inviteeType":"individuals",  "groupId":"", "guestList":[["userKey":"ef54983685274366ba339375ecda69df"], ["phoneNumber":"3106198542"], ["phoneNumber":"8188369592"]]] as [String : Any]
    
            let pickedImage : UIImage = UIImage.init(named: "ic_sample.png")!
            self.postComplexPictures(url:NSURL.init(string: "http://itaag-env-1.ap-south-1.elasticbeanstalk.com/create/event/")! as URL , params: dict, imageToUpload: pickedImage) { (arg0) in
                let (_, list, isSuccess) = arg0
                if(isSuccess)
                {
                    print(list)
                }
                else{
                    print(list)
                }
            }
        }
    

    And here is the uploading method:

     func postComplexPictures(url:URL, params:[String:Any], imageToUpload : UIImage, finish: @escaping ((message:String, list:[[String: Any]],isSuccess:Bool)) -> Void)
        {
            var result:(message:String, list:[[String: Any]],isSuccess:Bool) = (message: "Fail", list:[],isSuccess : false)
    
            let headers: HTTPHeaders
            headers = ["deviceid": "F139424D-C749-42F6-B804-21BD17E28CE0","userType": "personal","key": "c913136e897b419bab20746de7baab62", "Content-Type":"application/json"]
    
            AF.upload(multipartFormData: { (multipartFormData) in
                let jpegData = imageToUpload.jpegData(compressionQuality: 0.5)
                let jsonData = try! JSONSerialization.data(withJSONObject: params, options: [])
                multipartFormData.append(jsonData, withName: "eventdetails" )
                multipartFormData.append(jpegData!, withName: "eventImage",fileName: "file.jpg", mimeType: "image/jpg")
            }, to: url, usingThreshold: UInt64.init(), method: .post, headers: headers).response{ response in
    
                if((response.error == nil))
               {
                    do
                    {
                        if let jsonData = response.data
                        {
                            let parsedData = try JSONSerialization.jsonObject(with: jsonData) as! Dictionary
    
                            let status = parsedData["status"] as? NSInteger ?? 0
                            let msg = parsedData["message"] as? String ?? ""
    
                            print("parsedData=", parsedData)
                            
                            if(status==1) {
                                result.isSuccess = true
                                result.message=msg
                                if let jsonArray = parsedData["data"] as? [[String: Any]] {
                                    result.list=jsonArray
                                }
                            } else {
                                result.isSuccess = false
                                result.message=msg
                            }
    
                        }
                        finish(result)
                    } catch {
                       finish(result)
                    }
                } else {
                    print("Resonse.error",response.error?.localizedDescription as Any)
                    finish(result)
                }
            }
        }
    

    Please handle the response as per your convenience.

提交回复
热议问题