Hi guys, I wonder why the following isn't allowed:
this is Card.h
In the implementation card.m has this code:
SetCard is a subclass of Card so in the implementation of SetCard I would have liked to do this:
Instead the author goes through init in the sample code:
Why can't I use the lazy instantiation in SetCard.m and why must I go via init?
this is Card.h
Code:
#import <Foundation/Foundation.h>
@interface Card : NSObject
@property (nonatomic) NSUInteger numberOfMatchingCards;
@end
In the implementation card.m has this code:
Code:
- (NSUInteger)numberOfMatchingCards
{
if(!_numberOfMatchingCards)_numberOfMatchingCards = 2;
return _numberOfMatchingCards;
}
SetCard is a subclass of Card so in the implementation of SetCard I would have liked to do this:
Code:
- (NSUInteger) numberOfMatchingCards
{
if(!_numberOfMachingCards)_numberOfMatchingCards=3;
return _numberOfMatchingCards;
}
Instead the author goes through init in the sample code:
Code:
- (instancetype) init
{
self = [super init]
if(self){
self.numberOfMatchingCards=3;
}
return self;
}
Why can't I use the lazy instantiation in SetCard.m and why must I go via init?