views:

24

answers:

2

Hi, I have a custom data container object of type RowOfPlayerData. When I try to display a RowOfPlayerData object in NSLog, the system doesn't know how to display it, so it just shows the address of the object.

I am trying to figure out how i can display, in an NSLog statement, what is at that address so i can use the data.

I am trying to build this on a tutorial about NSPredicate: http://doronkatz.com/how-to-do-amazingly-simple-searches-with-nsar

I have tried to do some sort of description of the output but not succeded.

I have tried to figure out how to do this so i can access the data. Here is the test code i have used and some output i currently have. BTW, i am new to this.

I would really appreciate if someone could help me display the data from the playerArray in the right format as i just don't get this.

[CODE]

@interface RowOfPlayerData : NSObject {

    NSString    *playerName;
    NSString    *curGameType;
}

@property(nonatomic, retain)    NSString    *playerName;

@property (nonatomic, retain)   NSString    *curGameType;

-(void)addPlayerData2Array;

@end

here is the .m file:

-(void)addPlayerData2Array {


    NSLog(@">>addPlayerData2Array started<<");
    NSMutableArray *playerArray = [[NSMutableArray alloc] init];
    RowOfPlayerData *row;
    row = [[RowOfPlayerData alloc] init];
    row.playerName = @"Player1";
    row.curGameType = @"NORMAL1";
    NSLog(@"<#1> row.playerName: %@", row.playerName);
    NSLog(@"<#2> row.curGameType: %@", row.curGameType);
    [playerArray addObject:row]; 
    NSString *xx;
    xx = [playerArray objectAtIndex:0];

    NSLog(@"<#3> playerArray: %@", xx);

Output:

addPlayerData2Array started<<

2010-09-25 13:39:51.313 x1[3675:207] <#1> row.playerName: Player1

2010-09-25 13:39:51.314 x1[3675:207] <#2> row.curGameType: NORMAL1

2010-09-25 13:39:51.314 x1[3675:207] <#3> playerArray:

[/CODE]

A: 

If you want your object to be output as something more sensible when output via NSLog, then you simply need to implement the description method for your object.

For example:

- (NSString*)description {
    return [NSString stringWithFormat:@"Player: %@, GameType: %@", self.playerName, self.curGameType];
}

Then when you want to output it in a log:

NSLog(@"Row - %@", myRowOfPlayerData);
imaginaryboy
A: 

You have one problem here:

[playerArray addObject:row]; 
NSString *xx;
xx = [playerArray objectAtIndex:0];
NSLog(@"<#3> playerArray: %@", xx);

row is a RowOfPlayerData object, not a string. Following @imaginaryboy's advice, add a description method to your RowOfPlayerData class and make sure you get the class right when you're pulling objects out of the array.

RowOfPlayerData *xx = [playerArray objectAtIndex:0];
NSLog(@"<#3> playerArray: %@", xx);

All that being said, I don't think you're approaching this the correct way. You are calling an instance method -(void)addPlayerData2Array on an object, but then you are creating a brand new RowOfPlayerData and returning it in an array form. What you should be doing is this:

-(void)addPlayerData2Array {
    return [NSArray arrayWithObjects:self.playerName, self.curGameType, nil];
}

If you're calling this method on an object, you should probably be returning the data from that object.

kubi