Atma Atma - 2 years ago 155
Objective-C Question

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;


Here is the implementation:

#import "MyCycle.h"

@implementation MyCycle


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;


I allocate this object and populate it with the following:

MyCycle *cycle = [[MyCycle alloc] init];
cycle.myNumber = @1; = [[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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download