Amazon.com Widgets exercise 18-2, with <NSMutableCopying> question
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 22, 2014, 04:01:59 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Answers to Exercises
| | |-+ Chapter 18
| | | |-+ exercise 18-2, with <NSMutableCopying> question
Pages: [1] Go Down
Print
Author Topic: exercise 18-2, with <NSMutableCopying> question (Read 1489 times)
mdziedzic
Newbie
*
Posts: 41



WWW




on: 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
Code: (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
Code: (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
Code: (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
Code: (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
Code: (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







Logged
tadej5553
Full Member
***
Posts: 145


Email




Reply #1 on: 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
Last Edit: January 17, 2010, 03:23:33 AM by tadej5553 Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.