作者:你说Dan_795 | 来源:互联网 | 2024-11-27 20:59
学习Objective-C中,一般getter方法均实用if(!_variable)判断,在getter 方法中实例化。今天遇到了一个UNRECOGNIZED SELECTOR SENT TO INS
学习Objective-C中,一般getter方法均实用if(!_variable)判断,在getter 方法中实例化。
今天遇到了一个UNRECOGNIZED SELECTOR SENT TO INSTANCE 的错误。
排查了发现声明的属性,第一次判断!_varible即为NO,运行设置断点发现属性指针已经有值。
请解释一下这个现象?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| - (PlayingCardDeck *)playingCardDeck {
BOOL noInit =!_playingCardDeck;
NSLog(@"此处noInit 为 NO");
if (noInit) {
NSLog(@"getter is going");
_playingCardDeck = [[PlayingCardDeck alloc] init];
}
return _playingCardDeck;
}
- (Card *)testCard {
BOOL noInit =!_testCard;
NSLog(@"此处noInit 为 YES");
if (noInit) {
NSLog(@"getter is going");
_testCard = [[Card alloc] init];
}
return _testCard;
} |
PlayingCardDeck的init的代码,Card为默认。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @implementation PlayingCardDeck
- (instancetype)init {
self = [super init];
if (self) {
for (NSString *suit in [PlayingCard validSuits]) {
for (NSUInteger rank = 1; rank <= [PlayingCard maxRank]; rank++) {
PlayingCard *card = [[PlayingCard alloc] init];
card.rank = rank;
card.suit = suit;
[self addCard:card];
}
}
}
NSLog(@"init Finished");
return self;
}
@end |