I am learning cocoa from 4th ed Cocoa Programming for Mac OS X. I am doing an where I make a simple gui that reads in user text and add it to a list: I'm using a NSTextField and NSTableView respectively. The problem is it crashes randomly, I cant get anything to show up in NSMutableArray so I guess I have a memory leak somewhere. I was confused if NSMutableArray stored objects by copying or by reference:
GUI:
appdelegate.h:
appdelegate.m (GUI controller):
GUI:
appdelegate.h:
Code:
#import <Cocoa/Cocoa.h>
#import "NewTable.h"
@interface AppDelegate : NSObject <NSApplicationDelegate,NSTableViewDelegate>
{
NSInteger at;
int count;
NSMutableArray *list;
}
@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet NSTextField *input;
@property (assign) IBOutlet NSButton *add;
@property (assign) IBOutlet NSButton *remove;
@property (assign) IBOutlet NSTableView *table;
-(IBAction)addToList:(id)sender;
-(IBAction)removeFromList:(id)sender;
-(IBAction)checkList:(id)sender;
@end
appdelegate.m (GUI controller):
Code:
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize add;
@synthesize remove;
@synthesize input;
@synthesize table = _table;
- (void)dealloc
{
[list release];
[super dealloc];
}
-(id)init
{
[super init];
at=0;
count=1;
list = [[NSMutableArray alloc] init];
NSString *var = @"Type something";
[list addObject:var];
return self;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
}
-(void)tableView:(NSTableView *)aTableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
}
- (int)numberOfRowsInTableView:(NSTableView *)tableView
{
NSLog(@"numberOfRowsInTableView called");
return count;
}
- (id)tableView:(NSTableView *)tableView
objectValueForTableColumn:(NSTableColumn *)tableColumn
row:(int)row
{
NSLog(@"objectValueForTableColumn called");
at = [_table clickedRow];
if((int)at > count) at = (NSInteger) count;
else if(at<0) at = 0;
if(!count || (row>count))
{
return @"";
}
else
{
return [list objectAtIndex:row];
}
}
-(void)tableViewSelectionDidChange:(NSNotification *)notification
{
NSInteger row = [_table selectedRow];
if(row==-1)
{
return;
}
else
{
at = row;
}
}
-(IBAction)addToList:(id)sender
{
NSLog(@"addToList click registered, at=%ld, count=%i",at,count);
[list insertObject:[NSString stringWithString:[input stringValue]] atIndex:at];
count++;
}
-(IBAction)removeFromList:(id)sender
{
NSLog(@"removeFromList click registered at=%ld, count=%i",at,count);
if(at<count)
{
NSString *var = [list objectAtIndex:at];
[list removeObjectAtIndex:at];
[var release];
count--;
}
}
-(IBAction)checkList:(id)sender
{
NSString *the_list = [[NSString alloc] initWithString:@"List: "];
for (NSString *var in list)
{
[the_list stringByAppendingFormat:@"%@, %@",the_list,var];
}
NSLog(the_list);
[the_list release];
}
-(void)awakeFromNib
{
NSIndexSet *indices = [NSIndexSet indexSetWithIndex:1];
[_table selectRowIndexes:indices byExtendingSelection:NO];
[_table scrollRowToVisible:1];
at = count;
}
@end
Last edited: