0

I am new to Xcode and Swift, and I am trying to serialize JSON data from a url using the code below and get the error

'NSJSONReadingOptions' is not convertible to 'NSJSONWritingOptions'

on the line

var jsonResult = NSJSONSerialization.dataWithJSONObject(data, options: NSJSONReadingOptions.MutableContainers, error: nil)!

I cannot see my error, from online sources others use the same code and its works?

let task = session.dataTaskWithURL(loginUrl!, completionHandler: { (data, response, error) -> Void in
    if error != nil {
    }else {
        var jsonResult = NSJSONSerialization.dataWithJSONObject(data, options: NSJSONReadingOptions.MutableContainers, error: nil)!
        println(jsonResult)
    }
})

task.resume();
1
  • You're calling dataWithJSONObject which is used for serializing array/dictionary into JSON stored in a NSData object. But you want to call JSONObjectWithData, to deserialize JSON stored in NSData into an array/dictionary. The NSJSONReadingOptions is used with JSONObjectWithData. So, the problem isn't the NSJSONReadingOptions, but rather the fact that you called dataWithJSONObject instead of JSONObjectWithData. Commented May 17, 2015 at 23:38

1 Answer 1

1

Read the docs for dataWithJSONObject. The options parameter needs to be a value from the NSJSONWritingOptions enum, not the NSJSONReadingOptions enum.

But if your goal here is to convert an NSData object to an NSArray or NSDictionary, then the problem is that you are calling the wrong method. You want to use JSONObjectWithData, not dataWithJSONObject.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks.. Solve my problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.