Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Nivit Kochhar
Nivit Kochhar
2,502 Points

HTML to NSDictionary

I wanted to create a similar app to the blog reader app made in the tutorial.

Say I have a the html code of a webpage in the form of an NSString, how do I convert this to an NSDictionary object?

Do you want to insert it into the dictionary? or are you looking for each line to be an object in the dictionary? in which case wouldn't an array be better?

2 Answers

Nivit Kochhar
Nivit Kochhar
2,502 Points

I'm looking to make each tag the key and the information inside the tag to become the object of that key.

I don't know why you would want to do that, but I can see a lot of issues with it already. HTML has many tags that are the same, so parsing a tag may get over ridden by another tag down the line. Going through each nested element and storing all that data in a NSDictionary is bound to be a memory issue. Lastly HTML parsing is not a fun party. Not sure if Objective-C has an easy way to parse HTML but I've done it in PHP and Perl and I only do it on rare occasions where I need to screen scrap data, and then I only store data I need not the entire HTML structure. You may want to reconsider that approach.

Nivit Kochhar
Nivit Kochhar
2,502 Points

I managed to use the XMLParser class to get the job done. The app is working (y)