添加某些代码时,抛出类型Error函数的转换无效

时间:2016-05-03 17:45:46

标签: ios swift

此代码通常运行良好:

let session = NSURLSession.sharedSession()
        let request = NSMutableURLRequest(URL: NSURL(string: "http://example.com")!)
        request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
        request.HTTPMethod = "POST"
        let data = "devicetoken=\(devicetoken!)&userID=1"
        request.HTTPBody = data.dataUsingEncoding(NSASCIIStringEncoding)

        let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in

            if let response = response {

                let res = response as! NSHTTPURLResponse
                if (res.statusCode >= 200 && res.statusCode < 300)
                {

                    do {
                        let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers ) as! NSArray
                        let json = JSON(jsonData)

                        for (_, subJSON): (String, JSON) in json[0]["events"] {

                            let titlex = subJSON["title"].string
                            let guestx = subJSON["guests"].string

                            if let guestPicsArray = subJSON["guestpics"].array {
                                if (self.myarr.count > 0) {

                                    self.myarr.removeAll()

                                }

                                for item in guestPicsArray {
                                    if let title = item.string {
                                        self.myarr.append(title)
                                    }
                                }

                  let rel1 = InboxEvents(title: titlex!, guests: guestx!, eventresim: eventresimx!, eventID : NSInteger(eventIDx!)!, arr: self.myarr)
                  self.arrayOfRels.append(rel1)

                            }

                        }


                    } catch let error as NSError {
                        print(error)
                    }


                    dispatch_async(dispatch_get_main_queue(), {

                        self.tableView.reloadData()

                    })


                } else {
                    self.boxView.removeFromSuperview()
                    let alert = UIAlertController(title: "Sign in Failed!", message: "Connection Failed", preferredStyle: .Alert)

                    alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction) in

                    }))

                    self.presentViewController(alert, animated: true, completion: nil)
                }
            }  else if let error = error {
                print(error.localizedDescription)
        }
        })
        task.resume()

但是,当我将此代码添加到viewDidAppear时,我收到错误:

  

类型&#39;(_,_,_)抛出函数的无效转换抛出 - &gt; ()&#39;到非投掷函数类型&#39;(NSData?,NSURLResponse?,NSError?) - &gt;空隙&#39;

以上代码。

 if (PopupChat.instance().isUserLoaded == true) {
            let dialogsIDs: NSSet = NSSet(array: ["55fae39ca28f9a701d0058fb"])
            QBRequest.totalUnreadMessageCountForDialogsWithIDs(dialogsIDs as! Set<String>, successBlock: { (response: QBResponse, count: UInt, dialogs: [String : AnyObject]?) -> Void in
                let tabArray = self.tabBarController?.tabBar.items as NSArray!
                let tabItem = tabArray.objectAtIndex(3) as! UITabBarItem
                tabItem.badgeValue = String(count)
            }) { (response: QBResponse) -> Void in

            }
        }

1 个答案:

答案 0 :(得分:6)

更改

} catch let error as NSError {

简单

} catch {

那应该解决它。

我想你也想知道修复它的为什么?这是因为你的catch let error as NSError并不意味着你的意思。特别是,它不算作“全能”catch,因此无法捕捉到所有可能的错误。因此编译器会抱怨因为你的匿名函数可以抛出,这是不允许的。