1 Reply
      Latest reply: Feb 6, 2017 3:53 PM by eskimo RSS
      Falafel B Level 1 Level 1 (0 points)

        Hello.  New developer here.  I have the following code:

        func downloadItems() {
              
                let url: URL = URL(string: urlPath)!
                var session: URLSession!
                let configuration = URLSessionConfiguration.default
              
                session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
                let task = session.dataTask(with: url as URL)
              
                task.resume()
              
            }
          
            func URLSession(session: URLSession, dataTask: URLSessionDataTask, didReceiveData data: NSData) {
                self.data.append(data as Data);
              
            }
          
            func URLSession(session: URLSession, task: URLSessionTask, didCompleteWithError error: NSError?) {
                if error != nil {
                    print("Failed to download data")
                }else {
                    print("Data downloaded")
                    self.parseJSON()
                }
              
            }
          
            func parseJSON() {
              
                var jsonResult: NSMutableArray = NSMutableArray()
              
                do{
                    jsonResult = try JSONSerialization.jsonObject(with: self.data as Data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSMutableArray
                  
                } catch let error as NSError {
                    print(error)
                  
                }
              
                var jsonElement: NSDictionary = NSDictionary()
                let locations: NSMutableArray = NSMutableArray()
              
                for var i in 0 ..< jsonResult.count
                {
                  
                    jsonElement = jsonResult[i] as! NSDictionary
                  
                    let location = LocationModel()
                  
                    if let name = jsonElement["Name"] as? String,
                        let address = jsonElement["Address"] as? String,
                        let latitude = jsonElement["Latitude"] as? String,
                        let longitude = jsonElement["Longitude"] as? String
                    {
                      
                        location.name = name
                        location.address = address
                        location.latitude = latitude
                        location.longitude = longitude
                      
                    }
                  
                    locations.add(location)
                  
                }
              
                DispatchQueue.main.async(execute: { () -> Void in
                  
                    self.delegate.itemsDownloaded(items: locations)
                  
                })
            }
        

        Swift is giving me this error: 'URLSession' produces '()', not the expected contextual result type 'URLSession!' on line 7.  I'm not quite sure how to fix it, but I think it is some sort of syntax error with Swift 3.

        • Re: UISession Error Swift 3
          eskimo Apple Staff Apple Staff (6,505 points)

          Wow, that was way harder to find that it should have been.  You should definitely file a bug against the compiler diagnostics for this.  And please post your bug number, just for the record.

          What’s going on here is that you have the case of your delegate methods set incorrectly.  For example, you have:

          func URLSession(session: URLSession, dataTask: URLSessionDataTask, didReceiveData data: NSData) { 
              …
          }
          

          but it should be:

          func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
              …
          }
          

          The former defines a function called URLSession(session:dataTask:didReceiveData:) on your object.  Then, in downloadItems(), Swift interprets your attempt to construct a URLSession class as an attempt to call that function.  That function returns Void, so you get this error.

          The fix is to get the case of your delegate callbacks right.  The best way to do this is to let Xcode’s code completion fill them in for you.

          You might also want to take a look at the URLSession convenience routines (those that take a completion handler block).  Those are easier to wrangle if you’re just starting out.

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"