0

I have created one class which is subclass of NSObject(nsme as GroupClass).In that class I have created one property which is belonging from 'id' ,like

@property(nonatomic,retain)id myObj;

Now I am standing on way where I have one mutable array,that array contains instances of my GroupClass.so I am getting one copy in similar way

GroupClass* objG=[array objectAtIndex:i];

now I want one another copy of objG.I searched .And I found NSCopying Protocol.So I added as delegate NSCopying to GroupClass and also added copyWithZone method.Here it is

-(id)copyWithZone:(NSZone *)zone
{
  GroupClass *copy = [[[self class] allocWithZone: zone] init];
  copy.myObj=[myObj copyWithZone: zone];
  return copy;
}

Here I need deep copy.but it is always crashing after allocation line.Please help me.Thanking You.

Mayank Jain
  • 5,663
  • 7
  • 32
  • 65
  • myObj's class type is unknown so how can [myObj copyWithZone...] work? maybe myObj did not even implement copyWithZone method. You need to know what its type is, cast it to that type, and call its copyWithZone method if it exists. If id myObj is type of GroupClass, then it would cause an unending recursive call and crash – Nihat Apr 15 '14 at 06:36
  • then how can it work if I wrote [copy setmyObj:[self myObj]]; ?If I am using this line then it will allow to copy but that copy will shallow copy.And I need Deep copy. – user3428363 Apr 15 '14 at 06:41
  • No, it wont, it will only be a reference (shallow copy). What is the type of myObj? – Nihat Apr 15 '14 at 06:42
  • thats what I am saying if it is crashing because of 'id' type then it should not work with [copy setmyObj:[self myObj]]; also... – user3428363 Apr 15 '14 at 06:44
  • That statement will not give error but it wont "copy" it. – Nihat Apr 15 '14 at 06:45
  • What is its real type? Did you mean to pass more than one type of class there? – Nihat Apr 15 '14 at 06:46
  • it is...it gives me shallow copy.Ok so what is solution now? – user3428363 Apr 15 '14 at 06:47
  • You need to implement copyWithZone: method in all types of classes that you are storing in myObj. – Nihat Apr 15 '14 at 06:47
  • we have used ID because sometimes we get arrays,sometimes dictionaries and sometimes classes.that why we used ID – user3428363 Apr 15 '14 at 06:49
  • can you give me link of this kind of tutorial? – user3428363 Apr 15 '14 at 06:50
  • http://stackoverflow.com/questions/4089238/implementing-nscopying Regarding arrays for example, array will not deep copy its items, it will issue copywithzone message to its objects. If they implement it, then they will be deep copied. You may need to rethink about your design of this class. – Nihat Apr 15 '14 at 06:52
  • yes I think I need to restructure it..thanks a lot... – user3428363 Apr 15 '14 at 06:54

1 Answers1

0

Not all NSObject subclasses adopt the NSCopying protocol. The problem sounds like you are sending this message to an object which doesn't recognise it.

It doesn't matter that your GroupClass adopts NSCopying, that is not the problem. You are calling copyWithZone: on myObj, whatever that is.

This line of code:

copy.myObj=[myObj copyWithZone: zone];

Is almost certainly what is causing the crash.

EDIT

Not knowing what myObj is, I would recommend that if it is possible, and isn't a bad idea, that you subclass whatever the class is.

If it is a UIView for example, I would subclass it like so:

@class JVView : UIView <NSCopying>

I would then implement the copyWithZone: method appropriately, copying any of the properties of the instance that you feel are necessary.

Infinity James
  • 4,667
  • 5
  • 23
  • 36