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
MyController.m
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
SearchController.m
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:
The code for Employee.h:
and Employee.m:
Thanks to anyone that can think of anything that could be wrong....
First, the following example works fine:
I have a controller setup and the following works:
MyController.h
Code:
@interface MyController : NSObject {
NSMutableArray *employees;
}
-(IBAction)searchName:(id)sender;
MyController.m
Code:
-(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
Code:
@interface SearchController : NSObject {
}
+ (NSMutableArray *)searchName: (NSString *)searchString;
SearchController.m
Code:
@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:
Code:
-(IBAction)searchName:(id)sender {
[employees removeAllObjects];
NSMutableArray *theArray = [SearchController searchName:@"Test string"];
[employees addObjectsFromArray:theArray];
[theArray dealloc];
return;
}
The code for Employee.h:
Code:
@interface Employee : NSObject {
NSString *fullname;
NSString *title;
}
@property (copy) NSString *fullname;
@property (copy) NSString *title;
@end
and Employee.m:
Code:
@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....