tags:

views:

36

answers:

2

Having some issues with code not executing within the classes I created and thought I initialized and implemented correctly here are all the files. There is a class with an array of another class. Then implemented in the code finally but for some reason none of the NSLog calls seem to execute except the one immediately before [mobdefs createTable] in the main code. All help appreciated...

// Mobdefs.h
@interface Mobdefs : NSObject {

    @public NSMutableArray *mobInfo;
}

@property(retain) NSMutableArray *mobInfo; 

-(void) createTable;

@end

//  Mobdefs.m
#import "Mobdefs.h"
#import "Mobrec.h"

@implementation Mobdefs

@synthesize mobInfo;

- (id) init
{
mobInfo = [[NSMutableArray alloc] init]; 
return self;
}

-(void) addmobrec
{
MobRec *aNewMobRec = [[MobRec alloc] init];
aNewMobRec.mName=@"newbie";
[mobInfo addObject:aNewMobRec];  
[aNewMobRec release]; 
NSLog(@"MobRec Added\n");  
}

-(void) createTable 
{
NSLog(@"Populating mob table.\n"); // *** THIS CODE NEVER SEEMS TO GET EXECUTED
}

@end


//main.h
Mobdefs *mobdef;  


//main.m 
NSLog(@"just before createTable call\n");
[mobdef createTable];   

although the createTable code is called in the main the only NSLog output I get is the 'just before createtable...'

A: 

are you allocating and initializing mobdef in main.m?

ennuikiller
+1  A: 

It doesn't seem that you have initialized mobdef. Add the following:

mobdef = [[Mobdefs alloc] init];

to your main.m before you invoke the method on it.

Objective-C silently ignore calls on nil, as mobdef would be initialized to initially.

notnoop
hmmm I do this in main:Mobdefs *mobdef;I thought that when a pointer was created as such its init was called inherently. Is this not the case?
hmmm I do this in main: Mobdefs *mobdef; I thought that when a pointer was created as such its init was called inherently. Is this not the case? Ah I did that and it is now working, I guess I was wrong an explicit call to init needs to be fired. I really thought init was called automatically by the assign - funny how you can get warnings of an unused int but a non allocated class is ignored.
reference declaration outside methods (fields, static variables, etc) always get initialized to `nil` initially. You explicitly need to initialize them properly. references declared in methods without initialization could be assigned garbage and the compiler warns against that.
notnoop