tags:

views:

23

answers:

1

Hello,

I'm trying to unparse a string into an array, here is my code:

EDIT: I've found a bit more information.

startTimeArray = [[NSMutableArray alloc] init];  
NSString *tmpStartTimeString = [[tempItems objectAtIndex:0] valueForKey:@"temp_start_time"];
startTimeArray = (NSMutableArray *)[tmpStartTimeString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"|"]];

[test3 setText:[NSString stringWithFormat:@"%i", [startTimeArray count]]];      

The 'EXC_BAD_ACCESS' is deceiving, this is not where the problem is. The code above outputs the following details to the console:

UILable Test3 contains a 2 and the output from the array is as follows: Start Time: 2010-09-30 16:19:39 Start Time: 2010-09-30 16:19:43

At this point my array has values, so I then try to do the following:

startDateTime = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *strStartDateTime = [formatter stringFromDate:startDateTime];
[formatter release];

[startTimeArray addObject:strStartDateTime];

and this results in the following error:

-[__NSCFType addObject:]: unrecognized selector sent to instance 0x193370

I can see from Debugger, that strStartDateTime has a valid value.

Any thoughts ?

Regards, Stephen

+1  A: 

It looks like you're not retaining the returned array. Try this :

NSString *tmpStartTimeString = [[tempItems objectAtIndex:0] valueForKey:@"temp_start_time"];
startTimeArray = [[tmpStartTimeString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"|"]] retain];

[test3 setText:[NSString stringWithFormat:@"%i", [startTimeArray count]]];

PS If you definitely need it to be a mutable array, try this :

NSString *tmpStartTimeString = [[tempItems objectAtIndex:0] valueForKey:@"temp_start_time"];
NSArray *tempArray = [tmpStartTimeString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"|"]];
startTimeArray = [[NSMutableArray arrayWithArray:tempArray] retain];

[test3 setText:[NSString stringWithFormat:@"%i", [startTimeArray count]]];
deanWombourne
Thanks Dean, it was the retain I was missing.
Stephen