Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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)