Swift, NSJSONSerialization and NSError

后端 未结 5 1293
-上瘾入骨i
-上瘾入骨i 2021-02-12 16:19

The problem is when there is incomplete data NSJSONSerialization.JSONObjectWithData is crashing the application giving unexpectedly found nil while unwrappin

5条回答
  •  执笔经年
    2021-02-12 17:12

    Swift 3:

    let jsonData = Data()
    do {
        guard let parsedResult = try JSONSerialization.jsonObject(with: jsonData, options: .allowFragments) as? NSDictionary else {
            return
        }
        print("Parsed Result: \(parsedResult)")
    } catch {
        print("Error: \(error.localizedDescription)")
    }
    

提交回复
热议问题