I've read some resources on implementing NSCopying and copyWithZone, but things are still not very clear. I see many implementations with allocWithZone, which seems to be deprecated, and with autorelease, which seems to be unnecessary with ARC.
I was wondering if I can get away with something more simple. Something like this:
MTSound.h
#import <Foundation/Foundation.h> @interface MTSound : NSObject <NSCopying> @property (nonatomic) float frequency; @property (nonatomic) float amplitude; @property (nonatomic) float duration; // Designated Initializer - (instancetype)initWithFrequency:(float)freq amplitude:(float)amp duration:(float)dur; + (instancetype)soundWithFrequency:(float)freq amplitude:(float)amp duration:(float)dur; @end MTSound.m
#import "MTSound.h" @implementation MTSound // Initializers - (instancetype)initWithFrequency:(float)freq amplitude:(float)amp duration:(float)dur { if (self = [super init]) { _frequency = freq; _amplitude = amp; _duration = dur; } return self; } // Class factory methods + (instancetype) soundWithFrequency:(float)freq amplitude:(float)amp duration:(float)dur { return [[self alloc] initWithFrequency:freq amplitude:amp duration:dur]; } // NSCopying protocol - (id)copyWithZone:(NSZone *)zone { return [[self class] soundWithFrequency:self.frequency amplitude:self.amplitude duration:self.duration]; } @end Is there something wrong with this approach? If so, what's the correct implementation?