Objective c 编辑NSMutableArray中的一个对象也会更改NSMutableArray中的另一个对象
我有一个正常工作的导航应用程序。在表视图中,最后一个项目称为“添加项目”,如果用户按下它,它将创建一个新对象并将其传递给另一个视图,用户可以在该视图中输入该对象的详细信息。当用户返回上一屏幕时,新对象将显示在表中显示的数组中 我将其更改为“additem”字段始终是表中的第一个字段,而不是最后一个字段。我做了适当的更改,以便数组能够正确地显示在表上。然而,现在我注意到了奇怪的行为 如果编辑阵列中的第一个对象,则第七个对象也会更改为与此对象相同。如果编辑数组中的第二个对象,则第四个和第六个对象也将更改为相同。如果编辑数组中的第三项,第五个对象将更改为相同 会发生什么 在viewDidLoad:方法中,我初始化对象如下:Objective c 编辑NSMutableArray中的一个对象也会更改NSMutableArray中的另一个对象,objective-c,cocoa-touch,uitableview,object,nsmutablearray,Objective C,Cocoa Touch,Uitableview,Object,Nsmutablearray,我有一个正常工作的导航应用程序。在表视图中,最后一个项目称为“添加项目”,如果用户按下它,它将创建一个新对象并将其传递给另一个视图,用户可以在该视图中输入该对象的详细信息。当用户返回上一屏幕时,新对象将显示在表中显示的数组中 我将其更改为“additem”字段始终是表中的第一个字段,而不是最后一个字段。我做了适当的更改,以便数组能够正确地显示在表上。然而,现在我注意到了奇怪的行为 如果编辑阵列中的第一个对象,则第七个对象也会更改为与此对象相同。如果编辑数组中的第二个对象,则第四个和第六个对象也将
PersonDetails *personDetails = [[PersonDetails alloc] init];
这是当用户选择表上的行时执行的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
updatePersonArray = YES;
arrayIndex = indexPath.row-1;
editPerson = [[EditClassController alloc] initWithNibName:@"EditPerson" bundle:nil];
editPerson.title = @"Edit Person";
if (arrayIndex != -1) {
personDetails = [classArray objectAtIndex:arrayIndex];
}
else {
personDetails = [[PersonDetails alloc] init];
}
editPerson.personDetails = personDetails;
[self.navigationController pushViewController:editPerson animated:YES];
[editPerson release];
}
这就是视图将显示的样子。它将在编辑对象后更新表
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if ([personDetails isEmpty]) {
updatePersonArray = NO;
}
if (updatePersonArray) {
if (arrayIndex == -1) {
NSLog(@"adding new object to array");
[personArray addObject:personDetails];
}
else {
NSLog(@"replacing object at index %d", arrayIndex);
[personArray replaceObjectAtIndex:arrayIndex withObject:personDetails];
}
[self saveArrayToDisk];
[self.tableView reloadData];
updatePersonArray = NO;
}
else {
//load the array from disk
NSLog(@"loading array from disk");
NSData *theData = [[NSUserDefaults standardUserDefaults] objectForKey:@"personArray"];
if (theData != nil) {
NSLog(@"found something");
personArray = [[NSMutableArray alloc] initWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:theData]];
}
else {
personArray = [[NSMutableArray alloc] init];
}
}
}
编辑:我通过为person对象实现NSCopy,然后从数组中复制对象,而不是直接指向数组中的对象来解决问题。有人知道这解决了问题的原因吗?这一部分在内存所有权方面看起来有点可疑:
if (arrayIndex != -1) {
// here you get back an autorelease object - which you haven't retained
personDetails = [classArray objectAtIndex:arrayIndex];
}
else {
// here you create an object with retainCount =1
personDetails = [[PersonDetails alloc] init];
}
// depending on your property attribute this may or may not work as you expect
editPerson.personDetails = personDetails;
i、 e.@property(??)个人详细信息
编辑:我通过
为患者实施NSCopy
对象,然后制作
从数组中删除对象,而不是
直接指向中的对象
数组。有人知道为什么这解决了问题吗
有问题吗
最初的问题几乎肯定是在数组中多次出现相同的人员的问题。如果您要执行以下操作:
for (id p in myArray) NSLog("%p", p);
我敢打赌有些地址是相同的,多次表示数组中的同一对象
这就是为什么复制对象“修复”了问题。您在每次插入时都制作一份副本,以隐藏导致上述情况的狡猾逻辑。也许您可以显示一些代码?听起来您不是在创建对象,而是在引用数组中的同一对象。我在数组中创建了对象的副本,这解决了问题。在h文件中,我有@property(非原子,保留)PersonDetails*PersonDetails,然后我在m文件中合成它。好吧,那么你得到了一个漏洞,那么在上面的代码PersonDetails=[[PersonDetails alloc]init](这对你的问题没有帮助)中,你指的是哪个实例?你是说didSelectRowAtIndexPath中的那个吗?这是因为必须创建一个新对象,因为用户将添加一个新对象。