fatal error: unexpectedly found nil while unwrapping an Optional value in Swift when tried to parse JSON

前端 未结 3 2114
梦如初夏
梦如初夏 2021-01-25 17:18

I\'ve tried to build up a document-based Cocoa app and when I tried to parse JSON in readFromData: ofType: error: method, I got an error: fatal error: unexpec

3条回答
  •  北恋
    北恋 (楼主)
    2021-01-25 17:58

    Most likely the problem is that you have data that isn't actually JSON, so the deserialisation will return nil, or the data is an array, and converting it to a dictionary will obviously crash.

    You don't seem to understand some of the basics. What do you think AllowFragments is going to achieve? And why did you change error to nil? Do you understand what the error variable is there for? It's there to tell you what errors the JSON parser found. By setting the variable to nil, you prevent it from helping you.

提交回复
热议问题