• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Swift Autocomplete and closures
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Swift Autocomplete and closures


  • Subject: Re: Swift Autocomplete and closures
  • From: John Tsombakos <email@hidden>
  • Date: Tue, 10 Mar 2015 07:09:43 -0400

Not surprisingly, Xcode is able to autocomplete the parameters to the separate function.

Here’s my whole routine:

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let config = NSURLSessionConfiguration.defaultSessionConfiguration()

        let url = "">NSURL(string: "http://www.somewebservice.com")
        let session = NSURLSession(configuration: config)

        let request = NSURLRequest(URL: url!)

        var task = session.dataTaskWithRequest(request) {
            data, response, error in

            let httpResponse = response as! NSHTTPURLResponse

            if httpResponse.statusCode == 200 {

                let teams: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: nil)

                

                println(teams)
            }

            

        }
        task.resume()

    }

Paste this into a new project, and click after the “in” and see if Xcode recognizes “data”, “response”, or “error” when you start typing. Oddly, after you enter the keyword, you can option-click to have Xcode show you the correct type, and typing a “.” will give you the autocomplete popup.

Thanks.



On Mar 9, 2015, at 7:00 PM, LCS <email@hidden> wrote:

Hello...

There doesn’t seem to be anything wrong with your line of code, but there could be a problem somewhere else (or with how/where you’re writing the code for the completion handler block).

I’ve run into problems a few times where the compiler didn’t report errors properly or gave cryptic error messages, but I was usually able to figure out my mistake by breaking out the offending line into individual statements so that I got a better error message from the compiler (except for one time, where a mistake in declaring a function compiled cleanly and then later on segfaulted the compiler when it was called; 19437380).

To isolate the problem, I’d try declaring the completion handler function separately and then pass in the function.

func testHandler(myData: NSData!, myResponse: NSURLResponse!, myError: NSError!) -> Void
{
    // try your handler code here

     

}

    

let task = session.dataTaskWithRequest(request, completionHandler: testHandler)

If Xcode accepts your handler code that way and accepts the function as a parameter, you might have a mistake in how you’re adding your handler code to the inline closure in the line you posted. If you post the code showing how you are adding the handler code to the original line you posted, we could rule that out.

     Louis

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Swift Autocomplete and closures
      • From: LCS <email@hidden>
References: 
 >Re: Swift Autocomplete and closures (From: LCS <email@hidden>)

  • Prev by Date: Re: Swift Autocomplete and closures
  • Next by Date: Re: Swift Autocomplete and closures
  • Previous by thread: Re: Swift Autocomplete and closures
  • Next by thread: Re: Swift Autocomplete and closures
  • Index(es):
    • Date
    • Thread