Question

I have object copying issue.

@property (nonatomic,strong)ITEM *editingItem;
self.editingItem= nil;
self.editingItem = [[self.ItemsArray objectAtIndex:indexPath.row-1] copy];
self.editingrowIndex = indexPath.row;

When I assign some object to editingItem in didselect table row and start editing it in textfield delegates when i change the object property values then it is changning in the object stored in an array.but i want only editingItem object should have new values.But without updating array new values are getting stored from editingItem to object in the array.

Was it helpful?

Solution

The implementation provided by the OP, in the other answer is incorrect, as it does not copy the instance variables; rather it creates another reference to them. This would explain the issues the OP is seeing in his question (where that code should have been presented, and not in a separate answer).

Here is a better implementation:

- (id)copyWithZone:(NSZone *)zone
{
    Item *copy = [[Item allocWithZone:zone] init];

    copy->_nombre: [_nombre copy];
    copy->_linea = [_linea] copy];
    copy->_tags = [_tags copy];
    copy->_htmlSource = [_htmlSource copy];

    return copy;
}

Note that the instance variable copy statements provided in the other implementation is correct (aside from the missing copy call), but it won't work for private instance variables (a common occurrence), so I always stick to the form copy->_instanceVariable = [_instanceVariable copy];.

Also note that if the object derives from something other than NSObject then first statement should be:

Item *copy = [super copyWithZone:zone];

instead of:

Item *copy = [[Item allocWithZone:zone] init];

Further notes:

I find it highly suspect that you are keeping NSMutableString objects as instance variables. That is fairly unusual in my experience, having only used them as temporary objects for building immutable strings. I suspect you are not using the correct data type to hold this data.

OTHER TIPS

#import <Foundation/Foundation.h>

@interface Item : NSObject <NSCopying>

@property (strong, nonatomic) NSString *nombre;//nombre del medicamento
@property (strong, nonatomic) NSString *linea;//linea a la que pertenece
@property (strong, nonatomic) NSMutableString *tags;//palabras por las que se puede encontrar en el buscador
@property (strong, nonatomic) NSString *htmlSource;//código html para mostrar su contenido
@property (strong, nonatomic) NSMutableString *obj;

-(id) copyWithZone: (NSZone *) zone;

@end


@implementation TempObject


-(id) copyWithZone: (NSZone *) zone
{
    Item *copy = [[Item allocWithZone: zone] init];

    [copy setNombre: self.nombre];
    [copy setLinea: self.linea];
    [copy setTags: self.tags];
    [copy setHtmlSource: self.htmlSource];

    return copy;
}

Override CopywithZone method in your item class and set each and every property inside that method.It is the solution for copying object.When you do allocwithzone and init,the new object is created with new address and old values which you have to set manually in copywithzone: method.So duplicate instance with old values.It is perfect solution working for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top