Possible Duplicate:
NSMutableArray Not showing actual values when NSLog in iphone application
I have NSMutableArray initialised as follows:
- (id)init
{
self = [super init];
if (self)
{
mySpotsArray = [[NSMutableArray alloc]init];
}
return self;
}
The array stores data of NSTable as follows:
//-----------------------------------------
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return [mySpotsArray count];
[self saveMySpots];
}
//-----------------------------------------
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
Spot *sp = [mySpotsArray objectAtIndex:row];
NSString *identifier = [tableColumn identifier];
return [sp valueForKey:identifier];
[self saveMySpots];
}
//-----------------------------------------
- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
Spot *sp = [mySpotsArray objectAtIndex:row];
NSString *identifier = [tableColumn identifier];
[sp setValue:object forKey:identifier];
[self saveMySpots];
}
I am adding and deleting objects like this:
//-----------------------------------------
- (IBAction)addSpot:(id)sender
{
[mySpotsArray addObject:[[Spot alloc]init]];
[mySpotsTable reloadData];
[self saveMySpots];
}
//-----------------------------------------
- (IBAction)deleteSpot:(id)sender
{
NSInteger row = [mySpotsTable selectedRow];
[mySpotsTable abortEditing];
if (row !=-1)
{
[mySpotsArray removeObjectAtIndex: row];
}
[mySpotsTable reloadData];
[self saveMySpots];
}
I am saving and loading array content like this:
//-----------------------------------------
- (void) saveMySpots
{
savedSpots = [NSUserDefaults standardUserDefaults];
encodedMySpotsArrayObject = [NSKeyedArchiver archivedDataWithRootObject: mySpotsArray];
[savedSpots setObject: encodedMySpotsArrayObject forKey:[NSString stringWithFormat:@"MySpotsArrayKey"]];
}
//-----------------------------------------
- (void) loadMySpots
{
savedSpots = [NSUserDefaults standardUserDefaults];
decodedMySpotsArrayObject = [savedSpots objectForKey: [NSString stringWithFormat:@"MySpotsArrayKey"]];
mySpotsArray = [NSKeyedUnarchiver unarchiveObjectWithData: decodedMySpotsArrayObject];
}
Objects are encoded and decoded like this:
//-----------------------------------------
- (id)init
{
self = [super init];
if (self)
{
_mySpot = @"My Spot";
_localOffset = 0;
}
return self;
}
//-----------------------------------------
- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject: self.mySpot forKey:@"MySpotKey"];
[encoder encodeInt: self.localOffset forKey:@"LocalOffsetKey"];
}
//-----------------------------------------
- (id)initWithCoder:(NSCoder *)decoder
{
self = [super init];
if( self != nil )
{
self.mySpot = [decoder decodeObjectForKey:@"MySpotKey"];
self.localOffset = [decoder decodeIntForKey:@"LocalOffsetKey"];
}
return self;
}
Everything is properly defined in their respective .h like this
NSMutableArray *mySpotsArray;
NSUserDefaults *savedSpots;
NSData *encodedMySpotsArrayObject;
NSData *decodedMySpotsArrayObject;
All works perfect on UI level, i.e. the table is properly displayed, added, deleted, saved and loaded. But when I am trying to NSLog like this:
NSLog(@"%@", mySpotsArray);
I get this:
2012-09-19 13:41:25.372 Spot[1541:303] (
"<Spot: 0x100674ab0>",
"<Spot: 0x100674c20>",
"<Spot: 0x100675040>"
)
I've also tried this:
NSString *strData = [[NSString alloc]initWithData: decodedMySpotsArrayObject encoding:NSUTF8StringEncoding];
NSLog(@"strData: %@", strData);
and I get this:
2012-09-19 13:41:25.371 Spot[1541:303] strData: (null)
I simply need to access NSMutableArray content and then convert it to strings. The actual content what I see on UI is a table with 2 columns and 3 rows:
Yerevan 2
London -1
Los Angeles -9
What am I doing wrong?
Thanks