-[MyClassName copyWithZone:] unrecognized selector sent to instance

后端 未结 2 512
星月不相逢
星月不相逢 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:16

    Your -setObj1: method is declared as copy, so it calls -copy on your Class1 object. -copy just calls -copyWithZone:nil. So you either need to implement the NSCopying protocol (which means implementing -copyWithZone:), or change your property from copy to retain.

    0 讨论(0)
  • 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 <NSCopying>
    

    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)

    0 讨论(0)
提交回复
热议问题