Swift Json Parsing Error

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.