我在客观上很新,所以如果我的问题太蠢,请不要杀我^^
我的问题是:
*体系结构:*
我有一个带有属性的objet :来宾: listOfPricePaidByGuest --这个属性是一个NSNumber数组。所有的客人都在这个NSMutableArray : currentListOfBeneficiaries中
*情况:*
我有一个UIViewController,允许用户修改listOfPricePaidByGuest中的值
我希望允许用户取消所有修改。因此,在加载视图时,我将在一个新的mutableArray : tempListOfBeneficiaries中复制来宾(我已经实现了NSMutableCoying委托,并检查了这两个对象是否有不同的内存附加程序)。
当用户单击Save时,我只是删除视图,因为所有的修改都完成了。
当用户单击Cancel时,我只是将setArray方法用于原始的listOfPricePaidByGuest。
事实上,我tempArray中的所有tempArray也都被修改了,我不明白为什么.我尽我所能.
*问题:*
有人知道得到这些值不变的方法吗?
*一些代码:*
复制到tempArray
for (Guest *newGuest in self.currentGrocery.listOfBeneficiaries)
{
// Copying original guest
Guest *copyGuest = [newGuest mutableCopy];
for (NSNumber *aNumber in newGuest.listOfPricePaidByGuest)
{
int index = [newGuest.listOfPricePaidByGuest indexOfObject:aNumber];
// Copying NSNumber in newGuest.listOfPricePaidByGuest
NSNumber *newNumber = [aNumber copy];
// Replace current object
[copyGuest.listOfPricePaidByGuest replaceObjectAtIndex:index withObject:newNumber];
// Releasing current copied number
[newNumber release];
}
// Add it to tempArray
[self.tempListOfBeneficiaries addObject:copyGuest];
// Releasing current copiedGuest
[copyGuest release];
}*实现NSMutableCopying委托*
-(id)mutableCopyWithZone:(NSZone *)zone
{
Guest *guestCopy = [[Guest allocWithZone: zone] init];
[guestCopy setListOfGrocery:self.listOfGrocery];
[guestCopy setListOfPricePaidByGuest:self.listOfPricePaidByGuest];
[guestCopy setGuestName:self.guestName];
[guestCopy setGuestEmail:self.guestEmail];
[guestCopy setGuestNickname:self.guestNickname];
[guestCopy setGuestPhone:self.guestPhone];
[guestCopy setTotalGuestDepense:self.totalGuestDepense];
[guestCopy setTotalGuestDepenseToPayAfterReeq:self.totalGuestDepenseToPayAfterReeq];
[guestCopy setIsComming:self.isComming];
[guestCopy setIsSend:self.isSend];
return guestCopy;
}对不起英国人..。已经很晚了而且..。我是法国人,所以它解释了一切:P
(谢谢你的帮助:)
发布于 2012-04-08 01:06:03
问题在于数组,而不是NSNumbers。目前,你总是有一个数组,而不是两个。在[Guest mutableCopyWithZone:]中,必须复制listOfGrocery和listOfPricePaidByGuest数组:
[guestCopy setListOfGrocery:[self.listOfGrocery mutableCopy]];
[guestCopy setListOfPricePaidByGuest:[self.listOfPricePaidByGuest mutableCopy]];最好让setListOfGrocery和setListOfPricePaidByGuest执行这个复制步骤,除非有时需要保留输入数组。如果要用@property定义这些内容,只需使用替换assign或用copy替换retain即可。
另外,这是使用自动参考计数还是垃圾收集?如果没有,则需要在某个时候发布副本。
https://stackoverflow.com/questions/10059228
复制相似问题