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 Build a Self-Destructing Message iPhone App Designing and Starting the App Introducing the Project

initiating NSMutableArray with arrayWithObjects method.

Challenge question: Now switch to the implementation file and set the 'items' property to an array of two or more NSString objects using the 'arrayWithObjects' method of the NSMutableArray class.

The problem is in the implementation file.

header file:

@property (strong, nonatomic) NSMutableArray *items;

implementation file:

[self.items arrayWithObjects: @"one string", @"another string", @"third string"];

I also tried

[self.items arrayWithObjects: @"one string", @"another string", @"third string", nil];

What am I doing wrong?

2 Answers

Stone Preston
Stone Preston
42,016 Points

I believe arrayWithObjects is a class method, not an instance method. Your are assigning the value to your array incorrectly. try using it like so:

self.items = [NSMutableArray arrayWithObjects:@"one string", @"another string", @"third string", nil];

It worked, thanks.

Can you explain in simple english or with examples the difference between a class and instance method is? I've read the docs and a few videos on it and still don't understand.

Stone Preston
Stone Preston
42,016 Points

I can try. Instance methods work on instances, or objects. Class methods just use the class name. So arrayWithObjects is a class method, which means its called on the class, not the object like this: [NSMutableArray arrayWithObjects:@"blah", nil] .

an instance method is called on an actual object, or instance of a class. An example of this is the addObject method of the NSMutableArray class. This instance method acts on an actual object, or instance of the class like so: (assume that myArray has already been allocated and initialized: [myArray addObject: @"some string"].

notice how that in the first example, I used the name of the class itself, NSMutableArray, in the method call. In the second example, I used an actual instance of that class, a mutable array object or instance

class methods are used mainly for creating objects, aka constructors, such as the arrayWithArray method and others

ahhh ok! Thanks a lot for the explanation, I appreciate it.

Stephen Printup
Stephen Printup
UX Design Techdegree Student 45,252 Points

Also, Instance methods should be used to manipulate property values, such as 'hit points' in a hypothetical zombie game. We don't have the same ability to manipulate property values using Class methods.