解决方案 »

  1.   

    看文档里的解释:
    This method exists so class objects can be used in situations where you need an object that conforms to the NSCopying protocol. For example, this method lets you use a class object as a key to an NSDictionary object. You should not override this method.
    简单地说,是为了让"类"对象也符合NSCopying协议
      

  2.   

    From: http://oasku.com/?q-104.html
     
    首 先,从copy开始说,简而言之,copy的目的就是生成一个新的实例,然后把其成员都按原实例赋值。对于非指针型的成员,比如BOOL, int, float,这样的赋值可以直接进行。但是对于指针型的数据,比如Objc中用到的对象,就有Deep Copy和Shallow Copy的区别——这个和在C++中的基本上是一样的:是生成新的成员对象,或是指向同一成员对象。了 解了这点以后,再看看Copy在Objetive-C中的实现方式。如果要调用一个对象的copy方法,这个对象必须遵循NSCopying的协议。这个 协议中规定了一个方法:- (id)copyWithZone:(NSZone *)zone;我们就是通过实现这个方法给对象提供拷贝的功能。对于很多现有类,如NSString,NSDictionary,这个方法已经实 现。假设我们现在自定义了一个类,需要为这个类提供拷贝的功能,就需要自己来动手写CopyWithZone的方法:示例如下:这个是自定义的类:
    @interface Product : NSObject <NSCopying>
    {
        NSString *productName;
        float price;
        id delegate;
    }@end然后我们需要在Product类中实现NSCopying中的copyWithZone方法:
    - (id)copyWithZone:(NSZone *)zone
    {
        Product *copy = [[[self class] allocWithZone: zone]
                initWithProductName:[self productName]
                price:[self price]]; //注意这里,我们使用了class的allocWithZone的方法创建了一个拷贝,这里假定Product类中有一个 initWithProductName: price:的初始化方法。那么这样调用后就得到了一个Product的副本,而且name和price都已经设置好了    [copy setDelegate:[self delegate]]; //这里再设置delegate    return copy; //返回副本
    }那么这样,如果我们有一个product的实例, 假设为product1,然后调用Product *product2 = [product1 copy];
    就会使用我们上面写的copyWithZone的方法创建一个product1的副本,然后赋值给product2。这里再以上面方法中的成员delegate为例,解释一下deep copy和shallow copy:在 copyWithZone方法中,我们得到了一个新的product实例,但是delegate是个对象,所以在副本中,我们可以选择创建一个新的 delegate对象(deep copy),或是指向同一个delegate(shallow copy)。这个就取决于Product类中的setDelegate:方法了。你可以选择在setDelegate的时候,copy,也可以让它们都指 向同一个对象(但是需要retain,原因可以自己思考一下),当然,简单assign在某种情况下也是可以的。假设在Product类中有setDelegate:方法,或是有delegate的property:
    - (void)setDelegate: (id)aDelegate
    {
           [delegate release];
           delegate = [delegate copy];
    }这 样就是一个深拷贝了,因为使用了delegate的copy方法得到了一个delegate的副本。至于如何得到delegate的副本,就要看 delegate的copyWithZone方法的实现了,不在这个层面的考虑中。也就是说,copy总是一中“递归”的形式,从上到下,我们可以一层一 层的考虑。- (void)setDelegate: (id)aDelegate
    {
         [delegate release];
         delegate = [aDelegate retain];
    }
    这样操作后,delegate和aDelegate为同一对象,但是为了内存管理方面的要求,我们调用了retain来将reference count加了一。当然,如果不需要了,还可以直接赋值(assign):
    - (void)setDelegate: (id)aDelegate
    {
        delegate = aDelegate
    }你可以把这个例子自己实现一下,然后用log打一打内存,这个结构就很明了了。然后再说一下可变副本(mutable copy)和不可变副本(immutable copy):
    可变和不可变的概念,我们之前通过NSDictionary和NSMutableDictionary的区别了解过。
    一 般来说,如果我们的某个类需要区别对待这两个功能——同时提供创建可变副本和不可变副本的话,一般在NSCopying协议规定的方法 copyWithZone中返回不可变副本;而在NSMutableCopying的mutableCopyWithZone方法中返回可变副本。然后调 用对象的copy和mutableCopy方法来得到副本。举个例子:
    NSDictionary类已经遵循了NSCopying和NSMutableCopy的协议,也就是说我们可以调用它的copy和mutableCopy来得到不可变和可变的副本,程序如下:    NSDictionary *testDict = [[NSDictionary alloc]initWithObjectsAndKeys:@"hello", @"test",nil];
        NSDictionary *destDict = [testDict copy];
        NSLog(@"test Dict:%p,retain Count: %d\ndest Dict:%p, retain Count: %d",testDict,[testDict retainCount],destDict,[destDict retainCount]);这个在我机器上的运行结果为:
    test Dict:0x11f220, retain Count: 2
    dest Dict:0x11f220,retain Count: 2
    看 起来,两个dict指向了同一片内存区域,但是retainCount加了1。这点需要理解一下,因为我们使用NSCopying方法要返回一个不可变对 象。而且原来的testDict也是不可变的,那么这里的“副本”也就没多大意义了(这就如同使用字符串常量时,系统会为我们优化,声明了多个字符串,但 是都是常量,且内容相等,那么系统就只为我们申请一块空间,这个道理是一样的)。既然都不可变,那么指向同一个空间就可以了。这里的copy和 retain没什么区别。我们使用copyWithZone的方法返回immutable的对象,而不管原来的是可变的或是不可变的。我们再看一下如下代码:
        NSMutableDictionary *testDict = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"hello", @"test",nil];
        NSMutableDictionary *destDict = [testDict copy];
        NSLog(@"test Dict:%p,retain count:%d\ndest Dict:%p,retain count:%d",testDict,[testDict retainCount],destDict,[destDict retainCount]);
        [destDict setObject:@"what" forKey:@"test2"];NSMutableDictionary是可变的,该代码在我机器上运行的结果为:
    test Dict:0x20dcc0,retain count:1
    dest Dict:0x209120,retain count:1
    *** -[NSCFDictionary setObject:forKey:]: mutating method sent to immutable object
    可 以看到因为我们调用了可变对象的copy方法,这个不像之前的例子中一样,只是retain了一下。这里的test dict和dest Dict已经是两个对象了,但是,copyWithZone的方法返回的是不可变的对象,因此之后的setObject: forKey:方法会出现错误。下面这样改一下就OK了。    NSMutableDictionary *testDict = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"hello", @"test",nil];
        NSMutableDictionary *destDict = [testDict mutableCopy];
        NSLog(@"test Dict:%p,retain count:%d\ndest Dict:%p,retain count:%d",testDict,[testDict retainCount],destDict,[destDict retainCount]);
        [destDict setObject:@"what" forKey:@"test2"];
        NSLog(@"destDict:%@",destDict);
    运行结果为:
    test Dict:0x123550,retain count:1
    dest Dict:0x10a460,retain count:1destDict:{
        test = hello;
        test2 = what;
    因为我们使用了mutableCopy来得到了一个可变副本。Note:对于系统提供的所有既支持NSCopying,又支持NSMutableCopying的类。
    copy方法,得到的是不可变对象,不管以前的是可变还是不可变。
    mutableCopy方法,得到的是可变对象,不管以前的是可变还是不可变。
     
      

  3.   

    但是类对象需要这个干嘛呢,类对象又复制不了什么东西。
    上面已经说的很清楚了嘛,因为字典中的key对象需要遵循NSCopying协议,所以"类"对象也必须实现NSCopying协议才能作为key使用