3

so I'm trying to use a performRequestWithHandler block on a SLRequest object in my Swift iOS app and I can't deal with the NSError object. This is what how my code looks :

posts.performRequestWithHandler({(response:NSData!, urlResponse:NSHTTPURLResponse!, error:NSError!) in
    self.data = NSJSONSerialization.JSONObjectWithData(response, options: NSJSONReadingOptions.MutableLeaves, error: &error)
})

And I have an error on the &error that says : 'NSError' is not convertible to '@lvalue inout $T9' in Swift. Does anyone know what that means ?

Thank you in advance.

(I'm using Xcode Beta 6 v7 with OS X 10.10)

Que20
  • 1,469
  • 2
  • 13
  • 27

1 Answers1

5

You are reusing the error variable passed in to the block - you simply have to define a local optional variable and pass its reference to JSONObjectWithData

var myError: NSError?
self.data = NSJSONSerialization.JSONObjectWithData(response, options:NSJSONReadingOptions.MutableLeaves, error: &myError)

That happens because JSONObjectWithData needs a reference to a variable of NSError type. The one passed to the block is immutable - it points to an instance of NSError, but cannot be reassigned to point to another instance, or set to nil in case of no error.

Antonio
  • 71,651
  • 11
  • 148
  • 165
  • if you mean the `NSError!` argument in `performRequestWithHandler`, you could ideally declare it as `inout`, but you can't because the callback signature would change and you'll get a compilation error. If you not referring to that field... please clarify – Antonio Sep 06 '14 at 05:44