creating new object throws copywithzone unrecognized selector error in iOS
I have a simple object that looks like this:
#import <Foundation/Foundation.h> #import <Parse/Parse.h> @class MyUser; @interface MyCycle : NSObject @property (nonatomic, copy) NSNumber *myNumber; @property (nonatomic, strong) MyUser *user; @property (nonatomic, strong) NSArray *data; @end
Here is the implementation:
#import "MyCycle.h" @implementation MyCycle @end
Here is the user object:
#import <Foundation/Foundation.h> #import <Parse/Parse.h> @interface MyUser : NSObject @property (nonatomic, copy) NSString *usersName; @property (nonatomic, copy) NSString *gender; @property (nonatomic, copy) NSString *email; @property (nonatomic, copy) NSString *password; @property (nonatomic, copy) NSString *phoneNumber; @property (nonatomic, strong) UIImage *profileImage; @property (nonatomic, strong) PFFile *profileImageFile; @end
I allocate this object and populate it with the following:
MyCycle *cycle = [[MyCycle alloc] init]; cycle.myNumber = @1; cycle.data = [[NSArray alloc]init];
I get the following error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MyCycle copyWithZone:]: unrecognized selector sent to instance
Why is this happening and how can I fix it?
Somewhere in your code, you have a code that attempts to copy an MyCycle instance. Perhaps you use an object as a key to a dictionary? If you wish to continue with this behavior, you need to implement the NSCopying protocol for your class.
one of a reason, that this error could happen is The name of the variable you declare or your custom class is already used by the iOS SDK. You can say it’s a keyword. And the variable name can never be a keyword because it confuses your XCode (Objective C Compiler).
I know this has been answered for a while but for posterity here is an additional one that may be the why. Check your bindings. This also will happen if you accidentally bind the controller in Interface Builder and the UI control tries to use the actual controller as a string or other value type which causes the copyWithZone: to be issued.
For instance if File's Owner has a nested controller and you accidentally bind to innerController instead of innerController.property.
It's old question but probably you haven't @synthesized properties 'data', 'myNumber'. In other words you have no setter for those properties.