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 trialGerman Espitia
4,578 Pointsinitiating 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
42,016 PointsI 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];
Stephen Printup
UX Design Techdegree Student 45,252 PointsAlso, 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.
German Espitia
4,578 PointsGerman Espitia
4,578 PointsIt 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
42,016 PointsStone Preston
42,016 PointsI 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
German Espitia
4,578 PointsGerman Espitia
4,578 Pointsahhh ok! Thanks a lot for the explanation, I appreciate it.