PDA

View Full Version : Array not working from a method




rickpny
Feb 20, 2008, 11:29 AM
New to Objective-C so I am sure I am doing something wrong...

First, the following example works fine:
I have a controller setup and the following works:

MyController.h
@interface MyController : NSObject {
NSMutableArray *employees;
}
-(IBAction)searchName:(id)sender;


MyController.m
-(void)awakeFromNib
{
employees = [[NSMutableArray alloc] init];
}

-(IBAction)searchName:(id)sender {
[employees removeAllObjects];

Employee *emp = [[Employee alloc] init];
[emp setFullname:@"My name"];
[emp setTitle:@"My title"];
[employees insertObject:emp atIndex:[employees count]];
[emp dealloc];

emp = [[Employee alloc] init];
[emp setFullname:@"Second name"];
[emp setTitle:@"Second title"];
[employees insertObject:emp atIndex:[employees count]];
[emp dealloc];
}


This works fine and items are added to the array no problem.

The problem comes when I try to pull out this into a class method of another class called SearchController. The code for SearchController is:

SearchController.h
@interface SearchController : NSObject {

}

+ (NSMutableArray *)searchName: (NSString *)searchString;


SearchController.m
@implementation SearchController

+ (NSMutableArray *)searchName: (NSString *)searchString {
NSMutableArray *theArray = [[NSMutableArray alloc] init];

Employee *emp = [[Employee alloc] init];
[emp setFullname:@"My name"];
[emp setTitle:@"My title"];
[theArray insertObject:emp atIndex:[theArray count]];
[emp dealloc];

emp = [[Employee alloc] init];
[emp setFullname:@"Second name"];
[emp setTitle:@"Second title"];
[theArray insertObject:emp atIndex:[theArray count]];
[emp dealloc];

return theArray;
}

@end

The array never grows...

I don't think the issue is with the calling code because when I debug inside the new class I never see the array grow, but just in case here's how that is called:

New MyController.m:
-(IBAction)searchName:(id)sender {
[employees removeAllObjects];

NSMutableArray *theArray = [SearchController searchName:@"Test string"];
[employees addObjectsFromArray:theArray];
[theArray dealloc];
return;
}

The code for Employee.h:
@interface Employee : NSObject {
NSString *fullname;
NSString *title;
}

@property (copy) NSString *fullname;
@property (copy) NSString *title;

@end

and Employee.m:
@implementation Employee

@synthesize fullname;
@synthesize title;

- (id)init
{
self = [super init];
[self setFullname:@"The name"];
[self setTitle:@"Title"];
return self;
}
@end

Thanks to anyone that can think of anything that could be wrong....



kainjow
Feb 20, 2008, 01:12 PM
Do you have garbage collection enabled?

Either way, you should never call dealloc on a method. What you want here is the release method. dealloc gets called automatically by Cocoa when the object is being freed. You are only responsible for calling release.

Also you may want to go back and review memory management. If you don't have GC enabled you are leaking memory in a few places.

rickpny
Feb 20, 2008, 01:19 PM
Do you have garbage collection enabled?

Either way, you should never call dealloc on a method. What you want here is the release method. dealloc gets called automatically by Cocoa when the object is being freed. You are only responsible for calling release.

Also you may want to go back and review memory management. If you don't have GC enabled you are leaking memory in a few places.

Thanks very much for catching that... I've made the changes and should be OK now. (I do have GC'ing turned on so I am hoping that will handle any leaks I left open.)

Do you have any ideas on why I can't return an array the way I wanted? I tried a very similar method with a string and it worked fine. Of course since it seems the array isn't getting filled in the first place the problem probably resides there.

kainjow
Feb 20, 2008, 01:27 PM
Do you have any ideas on why I can't return an array the way I wanted? I tried a very similar method with a string and it worked fine. Of course since it seems the array isn't getting filled in the first place the problem probably resides there.

Ok let's start over with what's not working. I'm a little confused with the example code above since you seem to be changing things in the middle of your post :)

How are you testing to see if your array has objects? Is it being displayed in a table view? Are you just displaying it in the console? If you're using a table view and you're not using bindings, make sure you reloadData on your tableview when you modify your array.

rickpny
Feb 20, 2008, 01:50 PM
Ok let's start over with what's not working. I'm a little confused with the example code above since you seem to be changing things in the middle of your post :)

How are you testing to see if your array has objects? Is it being displayed in a table view? Are you just displaying it in the console? If you're using a table view and you're not using bindings, make sure you reloadData on your tableview when you modify your array.

Thanks! I was hovering over it, and for some reason it seems to work now. (I thought I ran it one more time after making the dealloc -> release change with it still not working, but it does now.

Thanks so much for the help.