Hi,
In the stanford course Paul Hegarty prefers to use lazy instantiation. For instance he makes a private declaration of
and then he uses the getter to perform an initialization
I'm cool with that. What I don't get though is that at another time Paul declares a public suit for a playingCard being:
but in the implementation he doesn't perform this lazy instantiation. So I don't understand where the alloc init of the suit string happens? (suit being a pointer to an NSString - object which ought to get a place in the heap)
In the stanford course Paul Hegarty prefers to use lazy instantiation. For instance he makes a private declaration of
Code:
@property (strong, nonatomic) (NSArray *)cards
and then he uses the getter to perform an initialization
Code:
- (NSArray *) cards
{
if(!_cards) _cards = [[NSArray alloc]init]
return _cards;
}
I'm cool with that. What I don't get though is that at another time Paul declares a public suit for a playingCard being:
Code:
@property (strong, nonatomic) NSString *suit;
but in the implementation he doesn't perform this lazy instantiation. So I don't understand where the alloc init of the suit string happens? (suit being a pointer to an NSString - object which ought to get a place in the heap)
Code:
#import "PlayingCard.h"
@implementation PlayingCard
@synthesize suit = _suit;
- (void)setSuit:(NSString *)suit
{
if ([@[@"♣︎", @"♠︎", @"♥︎", @"♦︎"]containsObject: suit]) {
_suit = suit;
}
}
- (NSString *)suit
{
return _suit? _suit: @"?";
}
@end