I’m quite new in Objective-c leaning so if my question is too stupid, please don’t kill me ^^
There’s my problem :
*Architecture : *
I have an objet : Guest with a property : listOfPricePaidByGuest
This property is an array of NSNumber.
All the guest are in this NSMutableArray : currentListOfBeneficiaries
*Situation : *
I have a UIViewController where i allow the user to modify the values in listOfPricePaidByGuest
I want to allow the user to cancel all the modification.
So when the view is loaded, i’m copying the guest in a new mutableArray : tempListOfBeneficiaries (i’ve already implemented the NSMutableCoying delegate and checked that the two objects have a different memory adress).
When the user click on Save, i’m juste removing the view, beacause all the modification are done.
When the user click on Cancel, i’m juste using setArray method to the original listOfPricePaidByGuest.
The fact is that all NSnumber in my tempArray are also modificated and i don’t understand why…
I’ve tried everything i could…
*Question : *
Does anyone know a method to get these values unchanged ?
*Some code : *
Copying to 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];
}
* Implementation for NSMutableCopying delegate *
-(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;
}
PS : Sorry for the english… It’s quite late and… Well i’m french so it explains everything 😛
Thanks for helping 🙂
The problem is in the array, not the NSNumbers. At the moment, you always have one array, never two. In
[Guest mutableCopyWithZone:], you must copy the listOfGrocery and listOfPricePaidByGuest arrays:It might be better still to have
setListOfGroceryandsetListOfPricePaidByGuestcarry out this copying step, unless there are times when you’ll need to retain the input array. If you’re defining these with@property, just use replaceassignorretainwithcopy.Also, is this using Automatic Reference Counting or Garbage Collection? If not, the copies need to be released at some point.