0

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.

11
  • Look here this may what you are looking for
    – Akhilrajtr
    Commented Apr 22, 2014 at 9:24
  • 3
    What is the object in self.ItemsArray and does it conform to the NSCopying protocol?
    – trojanfoe
    Commented Apr 22, 2014 at 9:25
  • why do you use copy of object instead of direct pointer to original one?
    – heximal
    Commented Apr 22, 2014 at 9:25
  • 1
    @heximal Presumably because he wants the user to edit the item and have the option of cancelling the edit.
    – trojanfoe
    Commented Apr 22, 2014 at 9:27
  • @trojanfoe you are correct.
    – iOSdev
    Commented Apr 22, 2014 at 9:37

2 Answers 2

0

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.

0
#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.

5
  • The total Description exists here stackoverflow.com/questions/1459598/…
    – iOSdev
    Commented Apr 23, 2014 at 6:31
  • Is this an answer? It looks like it should be part of your question, or it needs more explanation.
    – jrturton
    Commented Apr 23, 2014 at 6:46
  • @jrturton it is solution
    – iOSdev
    Commented Apr 23, 2014 at 7:15
  • @jrturton If it is not solution.. then please give me solution.I too don't know it worked this way.. if anything is wrong i will correct it.
    – iOSdev
    Commented Apr 23, 2014 at 7:17
  • Makes more sense now you've added some explanation!
    – jrturton
    Commented Apr 23, 2014 at 8:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.