Re: Problems with 2 classes HELP
Re: Problems with 2 classes HELP
- Subject: Re: Problems with 2 classes HELP
- From: Pete Yandell <email@hidden>
- Date: Sat, 14 Feb 2004 09:30:06 +1100
There are all sorts of things wrong with your code!
On 14/02/2004, at 12:44 AM, Roberto Sobachi wrote:
CollectionList *collection = [[CollectionLists alloc] init];
[collection loadXML];
NSArray *demo = [[NSArray alloc] init];
demo = [collection retrieveArray]; It returns a 0 count NSArray
You've just leaked memory here. You allocate an array and then
immediately replace it with another array. You need to go and read the
basic memory management docs again.
- (CollectionList *) init
{
[super init];
collection = [[NSMutableArray alloc] init];
return self;
}
You should never init like this. [super init] can return nil, so it
should be:
self = [super init];
if (self != nil) {
collection = [[NSMutableArray alloc] init];
}
return self;
You also need a matching dealloc method:
- (void)dealloc
{
[collection release];
}
- (void)loadXML
{
NSString *path = [@"~/Library/Demo/Demo.xml"
stringByExpandingTildeInPath];
collection = [[NSMutableArray arrayWithContentsOfFile: path] retain];
You've just leaked memory again by overwriting your pointer to
collection. You need a [connection release] before this line.
NSLog(@"%i", [collection count]); it Returns count Array > 0
This doesn't mean that you got an empty array. You may have failed to
load the array entirely and got a nil pointer back. Try NSLog (@"%@",
collection) and see what happens.
These are all very basic mistakes. Again, I really strongly recommend
that you go and get yourself a book on Cocoa and a good general
programming text. You'll learn this stuff a lot quicker that way.
Pete Yandell
http://pete.yandell.com/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.