0

I would like to convert my json response to an array but it doesn't work and I got an error that says my items in the array are nil

func getcomments(){
    RestApiManager.sharedInstance.getComments(TUTORIAL_ID: id){
        response in
        let comments = JSON(response)

        for item in comments.array!{
            let comment = Comment(memail:  String(describing: item["email"]), mcomment:  String(describing: item["comment"]), mcomment_date:  String(describing: item["comment_date"]), manswer:  String(describing: item["answer"]), manswer_date:  String(describing: item["answer_date"]))
            self.comments.append(comment)
        }
    }
}

it is my json response:

   [{
        "email": "-",
        "comment": "\u0627\u0632 \u0627\u067e\u0644\u06cc\u06a9\u06cc\u0634\u0646 \u062e\u0648\u0628\u062a\u0648\u0646 \u0645\u0645\u0646\u0648\u0646\u0645",
        "comment_date": "2017-07-15 19:30:00",
        "answer": null,
        "answer_date": null
    },
    {
        "email": "[email protected]",
        "comment": "salam",
        "comment_date": "2017-07-11 19:30:00",
        "answer": "\u062a\u0634\u06a9\u0631",
        "answer_date": "2017-07-12 03:50:57"
    }
]

I got nil error in this line:

unexpectedly found nil while unwrapping an Optional value

for item in comments.array!
4
  • Right here comments.array! If you're going to bang something you best be sure that it is not nil. I don't think you can guarantee that here. Obviously not, because it crashed. Commented Aug 14, 2017 at 9:20
  • 2
    What is the type of response? If it is a string you need to use JSON(parseJSON:) Commented Aug 14, 2017 at 9:25
  • @sweeper yes I needed JSON(parseJSON:) it works thank you Commented Aug 14, 2017 at 10:03
  • @fazed I posted as an answer. Please consider accepting it by clicking on that checkmark! Commented Aug 14, 2017 at 10:09

2 Answers 2

1

According to your comment, response is actually a string. Therefore, you can't just create a JSON using init(_:). You need init(parseJSON:).

init(_:) will just create a JSON with just that string instead of a JSON object, which is obviously not what you want. init(parseJSON:) will actually parse your JSON string and allow you to access the different key value pairs.

func getcomments(){
    RestApiManager.sharedInstance.getComments(TUTORIAL_ID: id){
        response in
        let comments = JSON(parseJSON: response)
Sign up to request clarification or add additional context in comments.

Comments

1

It's easier if you decode it as an array of structures.

First, create the struct:

struct Comment: Codable {
  var email: String
  var comment: String
  var comment_date: String
  var answer: String
  var answer_date: String
}

Then you can just call the JSON like that:

guard let url = Bundle.main.url(forResource: resource, withExtension: "json") else {
    throw Errors.couldNotFindResource
}
data = try! JSONDecoder().decode([Comment].self, from: Data(contentsOf: url))

Comments

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.