这是Deck.h的代码
#ifndef Machismo_Deck_h
#define Machismo_Deck_h
#import <Foundation/Foundation.h>
#import "Card.h"
@interface Deck : NSObject
-(void)addCard:(Card *)card atTop:(BOOL)atTop;
-(void)addCard:(Card *)card;
-(Card *)drawRandomCard;
#endif
下面是Deck.m的代码
#import "Deck.h"
@interface Deck()
//private methods are declared here
@property (strong, nonatomic) NSMutableArray *cards;
@implementation Deck
-(NSMutableArray *)cards{
if (!_cards) {
_cards = [[NSMutableArray alloc] init];
}
return _cards;
}
-(void)addCard:(Card *)card atTop:(BOOL)atTop{
if (atTop) {
[self.cards insertObject:card atIndex:0];
} else {
[self.cards addObject:card];
}
}
-(void)addCard:(Card *)card{
[self addCard: card atTop: NO];
}
-(Card *)drawRandomCard{
Card *randomCard = nil;//Parse Issue Expected expression
if ([self.cards count]) {
unsigned index = arc4random() % [self.cards count];
randomCard = self.cards[index];//Semantic Issue Use of undeclared identifier 'randomCard'
[self.cards removeObjectAtIndex:index];
}
return randomCard;//Semantic Issue Use of undeclared identifier 'randomCard'
}
@end
红色的是错误的地方,刚接触Objective-C~ 请老司机看看为什么报错~
我觉得你没有持有randomCard,身为局部变量,在if执行体中设置指针之后,很有可能在if执行后被释放掉,不确定啊只是感觉。。。<divclass='ref'>
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。