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 Objective-C Basics (Retired) Advanced Objective-C Categories

Chee Mervin
PLUS
Chee Mervin
Courses Plus Student 4,195 Points

Category object for incrementing all items in a mutable array

Hi guys, I was wondering how to add/increment certain ints within my NSMutableArray.

I have:

NSMutableArray *numbers = [[NSMutableArray alloc] init];
numbers[0] = @20;
numbers[1] = @40;
numbers[2] = @60;
NSLog(@"the numbers are %@",numbers);

So far I know that I can remove/add objects at a specific index, but lets say I would like to increase all the ints in my array by 2? And would also like to use the +2 increment to all items in the array for any other arrays in the future.

i know I would most probably have to make a new category object I'm also guessing that I'll need to use a for loop to traverse the array.

Which brings me to my question:

1 - how will I make sure that the for loop targets each individual array item, rather than add up the sum of all my array items(as shown in the earlier for loop video)?

Sorry if this as I'm still a bit hazy about categories, thanks! =)

Cheers

  • mervin

3 Answers

Change the value @ the index by itself + 2.

numbers[0] = numbers[0] + 2;

Chee Mervin
PLUS
Chee Mervin
Courses Plus Student 4,195 Points

Hi James, thanks so much for the answer!

Cheers! -mervin

Chee Mervin
PLUS
Chee Mervin
Courses Plus Student 4,195 Points

Hi James, thanks so much for the answer!

Cheers! -mervin