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

When and how should I use classes?

I'm going through the Crystal Ball app, and Amit places the prediction method in its own class called CrystalBall. I've also done the extra credit that changes the text colors randomly. Should I create a new class for that as well? It seems like if I were to create a new class for every method I want to create, there will be a ton of class files in a large app, and that seems rather inefficient.

Also, can someone explain what the underscore in this bit of code means, and why there's an asterisk inside the parentheses?

#import "THCrystalBall.h"
@implementation THCrystalBall

- (NSArray *) predictions {
    if (_predictions == nil) {
        _predictions = @[@"1", @"2"];
    }
    return predictions;
}

@end

1 Answer

Rutger Farry
Rutger Farry
10,722 Points

It's different for different projects, but in iOS programming, where you use the model-view-controller paradigm, you should usually create a separate class file for the model, view, and controller. As your project grows and gains new "screens" or "views", you should create a new pair of view and view controller classes.

Unless you have a very data-intensive app, the number of model classes you have will usually be less the the number of view/view controller classes, but as you implement new functionality, your number of model classes should grow to accommodate each distinct new functionality.

As you continue learning iOS development, you will notice patterns and knowing when to create new class files will become second nature.