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 trialMichael Gaynor
1,654 PointsDeclaring 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
RASHU BHATNAGAR
Courses Plus Student 2,669 PointsHi Micheal, according to my understanding...
- 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.
- 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.
- 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.....
RASHU BHATNAGAR
Courses Plus Student 2,669 PointsHi 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....
Michael Gaynor
1,654 PointsHey 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!
Michael Gaynor
1,654 PointsMichael Gaynor
1,654 PointsThat helps thanks!