-[MyClassName copyWithZone:] unrecognized selector sent to instance

后端 未结 2 555
星月不相逢
星月不相逢 2021-02-01 01:02

My application crashed with the reason:

-[MyClassName copyWithZone:] unrecognized selector sent to instance

I have two classes. Let

2条回答
  •  天命终不由人
    2021-02-01 01:29

    To make your class respond to copyWithZone:, you have to implement the NSCopying protocol in your class. And you must override the copyWithZone: method.

    For example:

    First you have to implement the NSCopying protocol in your interface declaration.

    @interface MyObject : NSObject 
    

    Then override the copyWithZone method like,

    - (id)copyWithZone:(NSZone *)zone
    {
        id copy = [[[self class] alloc] init];
    
        if (copy)
        {
            // Copy NSObject subclasses
            [copy setVendorID:[[self.vendorID copyWithZone:zone] autorelease]];
            [copy setAvailableCars:[[self.availableCars copyWithZone:zone] autorelease]];
    
            // Set primitives
            [copy setAtAirport:self.atAirport];
        }
    
        return copy;
    }
    

    I am glad if this helps you.

    (Reference)

提交回复
热议问题