Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Samppaa

macrumors regular
Original poster
Mar 26, 2010
168
23
Okay so I decided to make a little own project and after it progress in the book again, but I ran into problem as I got class called Character and I want it's name to show in the tableview that I made, I got mutablearray for those characters and I want it to display the name of character in tableview. I know that there might be other bad things in my code also that's why I am making this so I can learn to write apps on my own.. Here is the code

Character.h
Code:
//
//  Character.h
//  Characters
//
//  Created by Samuli Lehtonen on 8.6.2010.
//  Copyright 2010 Test. All rights reserved.
//

#import <Cocoa/Cocoa.h>


@interface Character : NSObject {
	NSMutableString * name;
	NSNumber * health;
	NSNumber * strenght;

}

-(void)setStats:(NSString *)charName;
-(NSString *)name;
@end

character.m
Code:
//
//  Character.m
//  Characters
//
//  Created by Samuli Lehtonen on 8.6.2010.
//  Copyright 2010 Test. All rights reserved.
//

#import "Character.h"


@implementation Character



-(id)init
{
	[super init];
	name = [[NSMutableString alloc] initWithString:@"Character"];
	health = [[NSNumber alloc] initWithInt:100];
	strenght = [[NSNumber alloc] initWithInt:10];
	return self;
}

-(void)dealloc
{
	NSLog(@"Dealloccing...");
	[name release];
	[health release];
	[strenght release];
	[super dealloc];
}

-(void)setStats:(NSString *)charName
{
	[name setString:charName];
}

-(NSString *)name
{
	return name;
}

@end

AppController.h
Code:
//
//  AppController.h
//  Characters
//
//  Created by Samuli Lehtonen on 8.6.2010.
//  Copyright 2010 Test. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import "Character.h"


@interface AppController : NSObject {
	NSMutableArray *characters;
	IBOutlet NSTextField * nameField;
	IBOutlet NSTextField * statusText;

}

-(IBAction)addItem:(id)sender;

@end

AppController.m
Code:
//
//  AppController.m
//  Characters
//
//  Created by Samuli Lehtonen on 8.6.2010.
//  Copyright 2010 Test. All rights reserved.
//

#import "AppController.h"


@implementation AppController

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

-(void)dealloc
{
	[characters release];
	[super dealloc];
}

-(IBAction)addItem:(id)sender
{
	
	if ([[nameField stringValue] length] > 0) {
		
		Character * tempChar = [[Character alloc] init];
		[tempChar setStats:[nameField stringValue]];
		[characters addObject:tempChar];
		[tempChar release];
		[statusText setStringValue:@"Character added!"];
	}
	else {
		[statusText setStringValue:@"Please insert the name!"];
	}

	
}

-(int)numberOfRowsInTableView:(NSTableView *)tv
{
	return [characters count];
}

-(id)tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row
{
	NSString *v = [characters objectAtIndex:row];
	return v;
}

@end
 
Yes, but I got mutablearray of my class called characters they are inside it, how I make it that way that the tableView looks the characters name and shows it, thats what I meant.

Well I added it and it shows error in console..
Code:
[Switching to process 2239]
Running…
2010-06-09 02:27:18.502 Characters[2239:a0f] -[Character copyWithZone:]: unrecognized selector sent to instance 0x10043d0a0
2010-06-09 02:27:18.504 Characters[2239:a0f] -[Character copyWithZone:]: unrecognized selector sent to instance 0x10043d0a0
2010-06-09 02:27:18.518 Characters[2239:a0f] -[Character copyWithZone:]: unrecognized selector sent to instance 0x10043d0a0
2010-06-09 02:27:18.519 Characters[2239:a0f] -[Character copyWithZone:]: unrecognized selector sent to instance 0x10043d0a0
2010-06-09 02:27:18.750 Characters[2239:a0f] -[Character copyWithZone:]: unrecognized selector sent to instance 0x10043d0a0
2010-06-09 02:27:18.751 Characters[2239:a0f] -[Character copyWithZone:]: unrecognized selector sent to instance 0x10043d0a0
2010-06-09 02:27:19.309 Characters[2239:a0f] -[Character copyWithZone:]: unrecognized selector sent to instance 0x10043d0a0
2010-06-09 02:27:19.310 Characters[2239:a0f] -[Character copyWithZone:]: unrecognized selector sent to instance 0x10043d0a0
2010-06-09 02:27:19.438 Characters[2239:a0f] -[Character copyWithZone:]: unrecognized selector sent to instance 0x10043d0a0
2010-06-09 02:27:19.439 Characters[2239:a0f] -[Character copyWithZone:]: unrecognized selector sent to instance 0x10043d0a0
2010-06-09 02:27:19.442 Characters[2239:a0f] -[Character copyWithZone:]: unrecognized selector sent to instance 0x10043d0a0
2010-06-09 02:27:19.444 Characters[2239:a0f] -[Character copyWithZone:]: unrecognized selector sent to instance 0x10043d0a0
 
Code:
-(id)tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row
{
	NSString *v = [characters objectAtIndex:row];
	return v;
}

Given that characters is holding Character instances, please explain how v can possibly be an NSString.

Learn how to use the debugger. If you knew how to use it, you'd be able to confirm that each of your tableview delegate methods was doing the correct thing, by setting a breakpoint at the method and stepping through what it's doing.

I haven't even seen you use NSLog() to print what an expected value is. You should at least learn how to do that.

As it now stands, it looks like your only debugging strategy is to post your code and wait for someone else to find the bug. This works for occasional bugs, or ones where you've exhausted other avenues. As a general strategy, it is not sustainable.
 
Thanks again.. I got it now.. I had to do
Code:
Character *v = [characters objectAtIndex:row];
return [v name];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.