目标 C:由于未捕获的异常“NSInvalidArgumentException"而终止应用程序 [英] Objective C: Terminating app due to uncaught exception 'NSInvalidArgumentException'

查看:72
本文介绍了目标 C:由于未捕获的异常“NSInvalidArgumentException"而终止应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用因错误由于未捕获的异常‘NSInvalidArgumentException’而终止应用,原因:‘-[CALayer firstName]:无法识别的选择器发送到实例 0x4d382c0’".

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

这就是我想要做的.

1) 我创建了一个Person"类来存储名字、姓氏、电子邮件地址和电话号码等信息

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

Person.m 文件:

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) 在可变数组中存储人员列表

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) 在表格视图中列出人员信息列表

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?

感谢您对此的帮助!

推荐答案

您因悬空指针而崩溃.您将 personArray 数组设置为您创建的数组,但随后您释放了该数组,将 personArray 作为悬空指针.根据您发布的内容,我假设 personArray 是该类的一个属性.如果是这样,请更改:

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;

self.personArray = array;

应该可以解决问题.您现在拥有的方式是将数组指针分配给 personArray 指针.您必须为该属性调用 set setter 方法以获得正确的内存管理.

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.

这篇关于目标 C:由于未捕获的异常“NSInvalidArgumentException"而终止应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆