I have a datatable which shows the list of contacts. when I start the application all the data is loaded correctly.But after selecting a contact, I am sometimes getting this exception :-
Program received signal: “EXC_BAD_ACCESS”.
and sometimes
-[UITableViewRowData isEqualToString:]: unrecognized selector sent to instance 0x391dce0
most probably for this code:-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
ExpenseTrackerAppDelegate *appDelegate = (ExpenseTrackerAppDelegate *)[[UIApplication sharedApplication] delegate];
Person *pe开发者_StackOverflow中文版rson = (Person *)[appDelegate.expensivePersonsList objectAtIndex:indexPath.row];
NSString *name = [NSString stringWithFormat:@"%@ %@" ,person.lastName , person.firstName];
cell.textLabel.text = name; return cell; }
If I replace these lines of code
NSString *name = [NSString stringWithFormat:@"%@ %@" ,person.lastName , person.firstName];
cell.textLabel.text = name;
with this code
cell.textLabel.text = person.lastName;
then everything works fine?
I dont know what exactly happens?
After looking and debugging around, i found that this code doesnt seem to work.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// RootViewController *detailViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:[NSBundle mainBundle]];
PersonnalDetails *detailViewController = [[PersonnalDetails alloc] initWithNibName:@"PersonnalDetails" bundle:[NSBundle mainBundle]];
//detailViewController.view = [[UITableView alloc] initWithNibName:@"PersonnalDetails" bundle:[NSBundle mainBundle]];
// ...
// Pass the selected object to the new view controller.
ExpenseTrackerAppDelegate *appDelegate = (ExpenseTrackerAppDelegate *)[[UIApplication sharedApplication] delegate];
NSInteger row = indexPath.row;
Person *person = [[appDelegate expensivePersonsList] objectAtIndex:row];
NSLog(@"%@", person.firstName);
detailViewController.selectedPerson = person;
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
this line
NSLog(@"%@", person.firstName);
throws EXC_BAD_ACESS.
I debugged the code and the list appDelegate expensivePersonsList
contains objects. can anybody tell me the possible cause?
I think you want to put in a symbolic breakpoint for "objc_exception_throw" so you can see what happens before the crash. Just go to the Run menu -> Manage Breakpoints -> Add Symbolic Breakpoint, and type in objc_exception_throw.
BTW, you are probably calling an object which is already deleted.
What is the call stack on EXC_BAD_ACCESS?
Also, you can just use NSString to get a formatted string, no need to use NSMutableString or cast it...
At last, I found out the problem. Actually, what i wanted to do was to make all the properties of the Person object as readonly, so that only in the init method the properties will be initialized. so the Person.h looked like:
@interface Person : NSObject {
NSInteger pid;
NSString const *firstName;
NSString const *lastName;
NSString const *phoneNumber;
NSString const *emailAddress;
}
@property (readonly) NSString *firstName;
@property (readonly) NSString *lastName;
@property (readonly) NSString *phoneNumber;
@property (readonly) NSString *emailAddress;
@property (readonly) NSInteger pid;
-(id)initWithName:(NSString *)n lastName:(NSString *)l phoneNumber:(NSString *)p emailAddress:(NSString *)e pid:(NSInteger)i;
@end
And all these values didn't retain the values.
This question was the main problem for all my errors: Initializing a readonly property
精彩评论