I have a Card class subclassed from UIView and a Deck class from NSObject. Card has a few integer properties on top of the inherited UIView ones and Deck has an NSMutableArray for holding some cards. After generating a deck of cards, I want to display a randomly selected card (by adding it to the superview). Before I do, I check to see if there is a card already, I call a method to release it before asking for a new one. But I get the warning in the title. Here's the code...
#import <UIKit/UIKit.h>
#import "Card.h"
#import "Deck.h"
@interface FlashTestViewController : UIViewController {
Deck* aDeck;
Card* aCard;
}
- (IBAction)generateDeck;
- (IBAction)generateCard;
- (void)fadeAway:(id)sender;
@end
#import "FlashTestViewController.h"
@implementation FlashTestViewController
- (IBAction)generateDeck {
if (aDeck != nil) {
[aDeck release];
}
aDeck = [[Deck alloc] initDeckWithOperator:@"+"];
}
- (IBAction)generateCard {
if (aCard != nil) {
[aCard fadeAway];
}
aCard = [aDeck newCardFromDeck];
[self.view addSubview:aCard];
}
- (void)fadeAway:(id)sender {
[aCard removeFromSuperview];
[aCard release];
}
I am a beginner at programming (other than Basic!) so I'm still wrapping my head around the whole object thing. Thanks for any help and/or advice!
EDIT: Here's the Card and Deck code...
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@class Card;
@interface Card : UIView {
int upperOperand;
int lowerOperand;
NSString* theOperator;
int theResult;
}
@property(nonatomic) int upperOperand;
@property(nonatomic) int lowerOperand;
@property(nonatomic, retain) NSString* theOperator;
@property(nonatomic) int theResult;
@end
#import "Card.h"
@implementation Card
@synthesize upperOperand;
@synthesize lowerOperand;
@synthesize theOperator;
@synthesize theResult;
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
self.backgroundColor = [UIColor redColor];
self.layer.cornerRadius = 15;
self.alpha = 0.3;
self.layer.borderColor = [[UIColor blueColor] CGColor];
self.layer.borderWidth = 4;
}
return self;
}
- (void)dealloc {
[super dealloc];
}
@end
#import <Foundation/Foundation.h>
#import "Card.h"
@class Deck;
@interface Deck : NSObject {
NSMutableArray* cards;
}
@property(nonatomic, retain) NSMutableArray* cards;
- (id)initDeckWithOperator: (NSString*)mathOper;
- (id)newCardFromDeck;
@end
#import "Deck.h"
@implementation Deck
@synthesize cards;
- (id)initDeckWithOperator: (NSString*)mathOper {
if (cards != nil) {
[cards release];
}
cards = [[NSMutableArray alloc] init];
for (int i=0; i<11; i++) {
for (int j=0; j<11; j++) {
Card* aCard = [[Card alloc] initWithFrame:CGRectMake(10, 10, 60, 80)];
aCard.upperOperand = i;
aCard.lowerOperand = j;
aCard.theOperator = mathOper;
aCard.theResult = i + j;
[cards addObject: aCard];
[aCard release];
}
}
return self;
}
- (id)newCardFromDeck {
int index = random() % [cards count];
Card* selectedCard = [[cards objectAtIndex:index] retain];
[cards removeObjectAtIndex:index];
return selectedCard;
}
@end