-2

Following the Treehouse tutorials, the app is supposed to access an array of strings and randomly return one upon the press of a button. I thought "_predictions" was the same thing as "self.predictions". When I changed all of the "self.predictions" to "_predictions" in the implementation of the NSString "randomPrediction", nothing was returned. What's the difference in using "self.predictions" and "_predictions"?

#import "SLWCrystallBall.h"

@implementation SLWCrystallBall

- (NSArray *) predictions {
    if (_predictions == nil) {
        NSLog(@"NBA team names accessed");
        _predictions = [[NSArray alloc] initWithObjects:
            @"The Oklahoma City Thunder", @"The San Antonio Spurs", @"The Miami Heat", 
            @"The Indiana Pacers", @"The Chicago Bulls", @"The Toronto Bulls", 
            @"The Boston Celtics", @"The Brooklyn Nets", @"The New York Knicks", 
            @"The Philadelphia 76ers", @"The Cleveland Cavaliers", @"The Detroit Pistons", 
            @"The Milwaukee Bucks", @"The Atlanta Hawks", @"The Charlotte Bobcats", 
            @"The Orlando Magic", @"The Washington Wizards", @"The Denver Nuggets", 
            @"The Minnesota Timberwolves", @"The Portland Trail Blazers", 
            @"The Utah Jazz", @"The Golden State Warriors", @"The Los Angeles Clippers", 
            @"The Los Angeles Lakers", @"The Phoenix Suns", @"The Sacramento Kings", 
            @"The Dallas Mavericks", @"The Houston Rockets", @"The Memphis Grizzlies", 
            @"The New Orleans Pelicans", nil];

    }
    NSLog(@"Team name returned");
    return _predictions;
}

- (NSString*) randomPrediction {
    int random = arc4random_uniform((uint32_t)self.predictions.count);
    return [self.predictions objectAtIndex:random];
    NSLog(@"Random prediction created");
}

@end

Here is the header file:

#import <Foundation/Foundation.h>

@interface SLWCrystallBall : NSObject {
    NSArray *_predictions;
}

@property (strong, nonatomic, readonly) NSArray *predictions;

- (NSString*) randomPrediction;


@end
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
  • 3
    Please search. There are plenty of duplicates of this question. – rmaddy Apr 14 '14 at 23:21
  • 1
    Crystal only has one "l". – Fogmeister Apr 14 '14 at 23:22
  • I searched, but I couldn't find an answer that accommodated my level of experience. I know that "Crystall" is only supposed to have one "l". Can I change the name of a class after it's already been set? Thanks. – SilentLoneWolf Apr 14 '14 at 23:29
  • @SilentLoneWolf The best way to rename a class is using Xcode's [Refractor](http://stackoverflow.com/questions/3066163/rename-or-refactor-files-in-xcode). – NobodyNada Apr 27 '14 at 22:04

1 Answers1

2

_predictions accesses the variable directly, while self.predictions calls an accessor instead. You should use self.predictions because of memory management benefits. In your case, the accessor creates the array, so it is practically necessary. Please note that in your -predictions method, you must use _predictions otherwise your accessor will just call itself.

NobodyNada
  • 7,529
  • 6
  • 44
  • 51