Still not sure when/why to use <NSMutableCopying>.
In the below I used <NSCopying> but was able modify the copy. I thought the copy was supposed to be immutable.
Obviously I'm have some confusion over <NSCopying> vs. <NSMutableCopying>. Any help would be greatly appreciated.
XYPoint.h#import <Foundation/Foundation.h>
@interface XYPoint : NSObject <NSCopying> {
double x;
double y;
}
@property double x, y;
-(void) setX: (double) xVal andY: (double) yVal;
-(XYPoint *) copyWithZone: (NSZone *) zone;
@end
XYPoint.m#import "XYPoint.h"
@implementation XYPoint
@synthesize x, y;
-(void) setX: (double) xVal andY: (double) yVal {
x = xVal;
y = yVal;
}
-(XYPoint *) copyWithZone: (NSZone *) zone
{
XYPoint *newPoint = [[XYPoint allocWithZone: zone] init];
newPoint.x = x;
newPoint.y = y;
return newPoint;
}
@end
Rectangle.h#import <Foundation/Foundation.h>
#import "XYPoint.h"
@interface Rectangle : NSObject <NSCopying> {
double width;
double height;
XYPoint *origin;
}
@property double width, height;
-(void) setOrigin: (XYPoint *) pt;
-(XYPoint *) origin;
-(double) area;
-(double) perimeter;
-(void) setWidth: (double) w andHeight: (double) h;
-(void) translate: (XYPoint *) vector;
-(void) dealloc;
-(Rectangle *) copyWithZone: (NSZone *) zone;
@end
Rectangle.m#import "Rectangle.h"
@implementation Rectangle
@synthesize width, height;
-(void) setOrigin: (XYPoint *) pt {
origin = [[XYPoint alloc] init];
[origin setX: pt.x andY: pt.y];
}
-(XYPoint *) origin {
return origin;
}
-(double) area {
return width * height;
}
-(double) perimeter {
return (width + height) * 2;
}
-(void) setWidth: (double) w andHeight: (double) h {
width = w;
height = h;
}
-(void) translate: (XYPoint *) vector {
origin.x = origin.x + vector.x;
origin.y = origin.y + vector.y;
}
-(void) dealloc {
[origin release];
[super dealloc];
}
-(Rectangle *) copyWithZone: (NSZone *) zone
{
Rectangle *newRectangle = [[Rectangle allocWithZone: zone] init];
[newRectangle setOrigin: [origin copy]];
newRectangle.width = width;
newRectangle.height = height;
return newRectangle;
}
@end
main#import <Foundation/Foundation.h>
#import "XYPoint.h"
#import "Rectangle.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Rectangle *myRect = [Rectangle new];
XYPoint *myPoint = [[XYPoint alloc] init];
[myPoint setX: 100 andY: 100];
[myRect setOrigin: myPoint];
[myRect setWidth: 42 andHeight: 200];
NSLog(@"myRect Origin: (%f, %f)", myRect.origin.x, myRect.origin.y);
NSLog(@"myRect width and height: %f, %f", myRect.width, myRect.height);
Rectangle *myRectCopy = [myRect copy];
NSLog(@"\n");
NSLog(@"Copy of myRect:");
NSLog(@"myRectCopy Origin: (%f, %f)", myRectCopy.origin.x, myRectCopy.origin.y);
NSLog(@"myRectCopy width and height: %f, %f", myRectCopy.width, myRectCopy.height);
myRectCopy.width = 25;
myRectCopy.height = 52;
myRectCopy.origin.x = -5;
myRectCopy.origin.y = -7;
NSLog(@"\n");
NSLog(@"After changing the copy:");
NSLog(@"\n");
NSLog(@"myRectCopy Origin: (%f, %f)", myRectCopy.origin.x, myRectCopy.origin.y);
NSLog(@"myRectCopy width and height: %f, %f", myRectCopy.width, myRectCopy.height);
NSLog(@"\n");
NSLog(@"myRect Origin: (%f, %f)", myRect.origin.x, myRect.origin.y);
NSLog(@"myRect width and height: %f, %f", myRect.width, myRect.height);
[myPoint release];
[myRect release];
[myRectCopy release];
[pool drain];
return 0;
}
myRect Origin: (100.000000, 100.000000)
myRect width and height: 42.000000, 200.000000
Copy of myRect:
myRectCopy Origin: (100.000000, 100.000000)
myRectCopy width and height: 42.000000, 200.000000
After changing the copy:
myRectCopy Origin: (-5.000000, -7.000000)
myRectCopy width and height: 25.000000, 52.000000
myRect Origin: (100.000000, 100.000000)
myRect width and height: 42.000000, 200.000000