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

Categories lesson in Objective C

(sorry, I would have posted directly in there but for some reason the "ask a question" button doesn't work when you click it.)

So in this lesson we created a category for a method that capitalizes the first letter of a string. In the method, we use (what I'm assuming is a foundation method "capitalizedString" on an object (cap) like this:

[cap addObject:[string capitalizedString]

my question is, does this foundation method not work on it's on in our .main file? wouldn't:

NSLog(@"%@", [letters capitalizedString] 

work on the object letters its own without creating our own category called "capitalizeStrings?" In this case letters is just an NSArray and cap is a mutable array. is that the difference?

here is a link to the lesson: http://teamtreehouse.com/library/objectivec-basics/advanced-objectivec/categories

2 Answers

Hi , too many questions, but i will try one by one. Basically he is just trying to show an example in which the strings in the NSArray letters are being taken one by one and then being capitalized and the new capitalized value is being stored in the mutable array cap.

NSLog(@"%@", [letters capitalizedString] the above line will work on a particular string and not on the whole array of string. You have to some how retrieve each string one by one from the letters array and then capitalize them individually. the [capitalizedString] method works on string values like this: NSLog(@"%@", [@"rashu" capitalizedString]);

Your question regarding NSArray and NSMutable array the major difference is that NSArray once defined, we cannot change its values or add or remove values from it, where as in NSMutable array we can add values, change them and remove them as well, after it has been defined and thats why Douglass used an NSMutable array rather than NSArray for cap, other wise the addObject wont even work with it. The reason behind this is that we have to retrieve each value from the letters array and change it and then add it to the cap(mutable) array.

I hope this answer solves some of your mysteries.

Indeed that does answer my question. Quite well! :)