[英]Method does not return what it should when called
I have a piece of code that returns an object of type Card
like so: 我有一段代码返回
Card
类型的对象,如下所示:
-(Card*) drawRandomCard {
if ([self.cards count]) {
unsigned index = arc4random() % self.cards.count;
Card *randomCard = self.cards[index];
[self.cards removeObjectAtIndex:index];
NSLog(@"%@", randomCard.description); **//returns a description and not null**
return randomCard;
} else {
NSLog(@"nil");
return nil;
}
}
This actually works fine when I use it in other functions, but there is a problem with this one below: 当我在其他函数中使用它时,这实际上可以正常工作,但是下面的这个存在问题:
- (IBAction)addCards:(UIButton *)sender {
int numberOfCardsToAdd = EXTRA_CARDS_NUMBER;
if ([[self.collectionView visibleCells] count] == 0) {
numberOfCardsToAdd = self.startingCardCount;
}
for (int i = 0; i < numberOfCardsToAdd; i++) {
if (!self.game.deck.isEmpty){
Card *cardToAdd = [self.game.deck drawRandomCard];
NSLog(@"%@", cardToAdd.description); **// gives (null)**
if (cardToAdd) { **// this does not get called**
// do stuff with cardToAdd
}
}
}
[self.collectionView reloadData];
}
So for some reason it seems my drawRandomCard
, when called with the method above, does not work. 因此出于某种原因,当我使用上述方法调用我的
drawRandomCard
时,似乎无法正常工作。 Does anyone know how to fix this? 有谁知道如何解决这一问题?
Thanks very much! 非常感谢!
Edit: my method for initializing the game object that has the deck
property: 编辑:我的用于初始化具有
deck
属性的游戏对象的方法:
- (id) initWithNumberOfCards: (int) numberOfCards withDeck: (SetsPlayingDeck *) deck {
self = [self init];
self.score = 0;
_deck = deck;
_cards = [[NSMutableArray alloc] initWithCapacity:numberOfCards];
for (int i = 0; i < numberOfCards; i++) {
[_cards addObject: [_deck drawRandomCard]];
}
return self;
}
This method is called right at the start of the program. 在程序开始时立即调用此方法。
Edit #2: 编辑#2:
Here is the code that initializes the game object, along with a deck object that is its property: 这是初始化游戏对象以及其属性的deck对象的代码:
- (SetsGame *) game {
if (!_game) {
SetsPlayingDeck *deck = [[SetsPlayingDeck alloc] init];
_game = [[SetsGame alloc] initWithCardCount:self.startingCardCount usingDeck:deck];
}
NSLog(@"%@", _game.deck.description); **// this returns null!!**
return _game;
}
As far as I can see, the issue should lie with the method initWithCardCount:usingDeck:
, where you probably forgot to assign the deck
argument to the deck
property (or for some reason the assignment is not executed): 据我所知,问题应该出在
initWithCardCount:usingDeck:
方法上,您可能忘记了将deck
参数分配给deck
属性(或者由于某种原因未执行分配):
_game = [[SetsGame alloc] initWithCardCount:self.startingCardCount usingDeck:deck];
So, please review this or post that method definition. 因此,请对此进行审查或发布该方法定义。
Maybe the problem is that you're running out of cards. 也许问题是您的卡用完了。 Inside your init method you have the following line inside a for-loop:
在init方法内部,for循环中包含以下行:
[_cards addObject: [_deck drawRandomCard]];
And inside your drawRandomCard
method you have the following line: 在
drawRandomCard
方法中,您具有以下行:
[self.cards removeObjectAtIndex:index];
I'm assuming that _cards
(1st line) and self.cards
(2nd line) are different objects. 我假设
_cards
(第一行)和self.cards
(第二行)是不同的对象。
Probably, by the time you call Card *cardToAdd = [self.game.deck drawRandomCard];
可能是在您致电
Card *cardToAdd = [self.game.deck drawRandomCard];
inside your addCards:
method you don't have any cards left. 在
addCards:
内部的方法中,您没有剩余的卡了。
Try to set a breakpoint and step in the for-loop and let me know, 尝试设置一个断点并进入for循环,让我知道,
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.