1

I load a custom object using :

NSArray* array = [[NSBundle mainBundle] loadNibNamed:@"MyViewItem" owner:nil options:nil];
MyViewItem* myBaseItem = [array objectAtIndex:0];

In order not to perform the loadNibNamed action like a hundred times in the several loops I have to do, I plan to use this object as a "base" object that i'll fill with differents informations, but always based on the same design (like in an UICollectionView actually).

My first move was to set in my loop something like :

for (int i = 0; i < x; i++ ) {
    MyViewItem* newItem = [myBaseItem copy];
}

Unfortunately, MyViewItem class tried to accessed "copyWithZone", which i didn't implement. On this point, i tried to inherits NSObject on it (I think all NSObject implements "copy" method), but the issue remains with the same error. So I tried to inherits NSCopying, and to implement my copyWithZone method, which is the following (not working though) :

@interface MyViewItem : UIView <NSCopying> 
    // some stuff

@end
@implementation MyViewItem
// some stuff
- (id)copyWithZone:(NSZone *)zone
{
    MyViewItem* copy = [[MyViewItem allocWithZone:zone] init];
    // What should i do here ?
    return copy;
}
@end

What am I missing ? I've read many issues concerning copy, but I couldn't solve mine :( Thanks !

Kleioz
  • 294
  • 3
  • 13
  • 1
    possible duplicate of [Best practice when implementing copyWithZone:](http://stackoverflow.com/questions/9907154/best-practice-when-implementing-copywithzone) – Volker Mar 18 '14 at 09:55
  • Actually, maybe i missed to explain the core of my issue : what i want to do is using a single .xib to generate multiple object with. And my first approach has been to trying to copy it. I don't especially HAVE to use a copy. – Kleioz Mar 18 '14 at 10:31
  • you should consider thinking through your problem and then ask the question you need an answer for... yet if copyWithZone: is called on your object, work through the answer i linked... – Volker Mar 18 '14 at 10:34
  • Well I did consider other options, but here i'm looking for informations on this solution. Considering your link (that I already saw), the main difference here is that I have to copy an object created with a "loadNibNamed" native method, and not a "simple" custom class. – Kleioz Mar 18 '14 at 13:37

0 Answers0