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 Build a Blog Reader iPhone App Getting Data from the Web What is NSDictionary?

Declaring properties

Hi, I was wondering why we didn't have to declare the NSDictionary objects (blogPost 1, 2 etc) as properties in our TableViewController header file as we did with the NSArray (*blogPosts - formerly *titles).

Thanks!

2 Answers

Hi Micheal, according to my understanding...

  1. If we don't define the variables (which could be basic data types (like int, float ) or NSArrays, or dictionaries etc) as properties then we will not be able to access them from outside the class. 2.More over making them as properties will make it easier for the programmer to set and retrieve their values without going through the formal process of defining the setter and getter functions for them.
  2. Defining the variables in the header file makes them public that is they could be accessed by outside classes and defining them in the implementation file makes them private that is they cannot be accessed outside the class.
  3. I usually prefer making the variables of a class as properties because first- i can easily access them without setter and getter, and second- they could be easily accessed outside the class as passing there value or getting their value outside the class is a lot easier...

I hope it helps you more in understanding the properties.....

That helps thanks!

Hi Micheal, I am a little to ios development, but will still try to solve your query.

Since the array will be holding just a single value that is the titles , it is more convenient at this point to declare just a single array of titles which is altogether an array of strings( since titles are nothing but string values). But later in the course you will see that the dictionary is being used to hold all the values (ie title, author, date, url) and those dictionary objects are being stored in an NSMutableArray. An array will more easily hold all the objects( each object being a dictionary) and since it is a NSMutable type , it will be less tedious and more manageable. Consider if we have 1000 posts then creating a property for each and every dictionary object will be a mess!!!

I really hope my answer would be able to solve some of your confusion....

Hey Rashu, thanks for your reply!

I understand now why we are using the NSDictionary / MutableArray to store the blog posts - I just think I was confused because we declared the NSArray in the .h file as a property - which as far as I know creates our setter / getter methods. I was just wondering why we didn't have to do the same for the NSDictionary.

A later video I watched led me to think that if we don't need to access the object outside of it's class we don't need to define setter / getters or declare it in the header, but I don't know if I am right about that.

Thanks for your help!