Objective C: Terminating app due to uncaught exception 'NSInvalidArgumentException'

安稳与你 提交于 2020-01-06 04:31:32

问题


My app is crashing with the error "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CALayer firstName]: unrecognized selector sent to instance 0x4d382c0'".

This is what I was trying to do.

1) I have created a 'Person' class to store information like firstName, lastName, email add and telephone number

Person.m file:

@implementation Person
@synthesize firstName, lastName, email, phoneNumber;

-(Person *)initWithfirstName:(NSString *)thisFirstName lastName:(NSString *)thisLastName email (NSString *)thisEmail phoneNumber:(NSString*)thisPhoneNumber
{ 
    if(self = [super init]) 
    {
        self.firstName = thisFirstName;
        self.lastName = thisLastName;
        self.email = thisEmail;
        self.phoneNumber = thisPhoneNumber;
    }
return self;
}
@end

2) Store a list of people in a mutable array

Person *firstPerson = [[Person alloc] initWithfirstName:@"Zen" lastName:@"Yong" email:@"" phoneNumber:@""];
person = firstPerson;

NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:person, nil];

personArray = array;

[array release];

3) List the list of person's information in a table view

int row = [indexPath row];
Person *aPerson = (Person *)[self.personArray objectAtIndex:row];

cell.textLabel.text = aPerson.firstName;

Can you help to advise why I am getting the error listed at the beginning of the thread?

Appreciate your help on this!

Zhen


回答1:


You're crashing due to a dangling pointer. You set personArray array to be the array you created, but then you release the array, leaving personArray as a dangling pointer. From what you posted I'm assuming personArray is a property on the class. If so, changing:

personArray = array;

to

self.personArray = array;

should fix the problem. The way you have it now the array pointer is just being assigned to the personArray pointer. You have to call set setter method for the property to get the proper memory management.




回答2:


This problem typicaly occurs when you have over released an object and the space has been reused for something else (a CALayer in your case). I can't see the specific problem here that might be causing that, but you have a definite bug:

Person *firstPerson = [[Person alloc] initWithfirstName:@"Zen" lastName:@"Yong" email:@"" phoneNumber:@""];
person = firstPerson;

NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:person, nil];

personArray = array;

[array release];

That creates an array and then immediately releases it. It's likely that the array has been deallocated. The person object in it, however, has not been released, so unless you do it later, it leaks. You probably wanted something more like:

Person *firstPerson = [[Person alloc] initWithfirstName:@"Zen" lastName:@"Yong" email:@"" phoneNumber:@""];

NSMutableArray *array = [[NSMutableArray alloc] initWithObject: firstPerson];

[firstPerson release];

You then have to save the array somewhere, so if personArray is an instance variable, you can just do

personArray = array; // personArray must be nil beforehand to stop leaks.

or if it is a property (this is better):

[self setPersonArray: array]; // or equivalently self.personArray = array;
[array release];

On a final unrelated note, it is considered better not to use properties in the -init or -dealloc methods. Your -init should probably look like:

if(self = [super init]) 
{
    firstName = [thisFirstName copy];
    lastName = [thisLastName copy];
    email = [thisEmail copy];
    phoneNumber = [thisPhoneNumber copy];
}

And don't forget to release everything in -dealloc.



来源:https://stackoverflow.com/questions/5713450/objective-c-terminating-app-due-to-uncaught-exception-nsinvalidargumentexcepti

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!