Hey all,
I've been playing around with Objective C lately and have 2 questions that reference this code.
AND
My questions are:
1. Why do I get compiler warnings about memory leaks in the first code chunk if I only use [pool release] ? All the code samples I've seen seem to make this a catch all for memory management. Is that not the case?
2. Why does the addCard method not actually add anything to the collectionOfCards? Shouldn't the addCard implementation do just that? When I view the collectionOfCards in the Xcode debugger after performing the add, it says I have 0 elements in the dictionary.
I would appreciate any help. This code seems pretty straightforward to me, so I'm a bit baffled.
I've been playing around with Objective C lately and have 2 questions that reference this code.
Code:
#import "Private_TutorAppDelegate.h"
#import "Card.h"
#import "CardCollection.h"
#import "MemoryType.h"
@implementation Private_TutorAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableString *aBackSide = [[NSMutableString alloc] init];
[aBackSide setString:@"1 Back Side"];
Card *aCard = [[Card alloc] init];
[aCard setFrontSide:@"1 Front Side"];
[aCard setBackSide:aBackSide];
CardCollection *aCardCollection = [CardCollection new];
[aCardCollection addCard: aCard];
[aCard release];
[aBackSide release];
[aCardCollection release];
[pool release];
}
@end
AND
Code:
#import "CardCollection.h"
@implementation CardCollection
//Add card to the collectionOfCards
//Returns void.
-(void) addCard:(Card *)inputCard
{
NSString *inputFrontSide = [inputCard getFrontSide];
[collectionOfCards setObject:inputCard forKey:inputFrontSide];
}
Code:
@interface CardCollection : NSObject {
NSMutableDictionary *collectionOfCards;
}
//Add card to the collectionOfCards
//Returns void.
-(void) addCard:(Card *)inputCard;
My questions are:
1. Why do I get compiler warnings about memory leaks in the first code chunk if I only use [pool release] ? All the code samples I've seen seem to make this a catch all for memory management. Is that not the case?
2. Why does the addCard method not actually add anything to the collectionOfCards? Shouldn't the addCard implementation do just that? When I view the collectionOfCards in the Xcode debugger after performing the add, it says I have 0 elements in the dictionary.
I would appreciate any help. This code seems pretty straightforward to me, so I'm a bit baffled.