Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan

Old Stuff => Chapter 18 => : mdziedzic January 14, 2010, 09:31:39 PM



: exercise 18-2, with <NSMutableCopying> question
: mdziedzic January 14, 2010, 09:31:39 PM
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
: (Objective-C)
#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
: (Objective-C)
#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
: (Objective-C)
#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
: (Objective-C)
#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
: (Objective-C)
#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









: Re: exercise 18-2, with <NSMutableCopying> question
: tadej5553 January 17, 2010, 03:19:44 AM
You should use <NSMutableCopying> when you have to similar classes, the only difference is that one is mutable and the other imutable.
In this case the object the copy method creates is mutable, but since there is only one type of rectangle class you should just use <NSCopying> to avoid confusion


Sorry, the copyright must be in the template.
Please notify this forum's administrator that this site is missing the copyright message for SMF so they can rectify the situation. Display of copyright is a legal requirement. For more information on this please visit the Simple Machines website.