Yesterday, when developing the company's iOS program, I encountered a JSON parsing problem, and it was a very strange question.
Because the original code is more complex, so the code is a little simplified, the code is as follows:
Server return Format (PHP):
Array (' title ' = ' Sina ', ' address ' = ' http://www.sina.com.cn '), Array (' title ' = ' + ' NetEase ', ' address ' = ' h ') Ttp://www.163.com '));
The code for the parsing section is as follows:
var jsondata:anyobject! = nsjsonserialization.jsonobjectwithdata (data, options:nsjsonreadingoptions.mutableleaves,error:nil); var links = [Link] (); If let Linksdata = Jsondata as? Nsarray {for linkdata:anyobject! in Linksdata { if let linkdict = LinkData as? nsdictionary {let link = link (); Link.title = linkdict["title"] as String; Link.address = linkdict["Address"] as String links.append (link);}}
Originally this is a very simple parsing, and there is no problem when executing here, but the key is that the application will report a bad_access error access after execution is complete.
There are a number of methods behind it, several methods are tried later, there is no error in writing another method, and check the difference in this code:
For linkdata:anyobject! In Linksdata
No error code:
For LinkData in Linksdata
Specific reasons, because I am not very familiar with Swift, so it is not clear.
But the following will all: anyobject! removal, found that the program did not appear bad_access error.
Make a note of it and hope that everyone who meets this question will help.
Swift Json Parsing Error