Amazon.com Widgets Synthesizing XYPoint
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 24, 2014, 11:01:36 AM
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
|-+ Programming in Objective-C, 4th edition
| |-+ Chapter 8
| | |-+ Synthesizing XYPoint
Pages: [1] Go Down
Print
Author Topic: Synthesizing XYPoint (Read 901 times)
jonthornham
Full Member
***
Posts: 169



WWW Email




on: April 29, 2012, 07:10:34 PM

I am working through the XYPoint example and I synthesized XYPoint instead of creating my own setter method.  When I synthesize XYPoint it appears the synthesized setter method is copying a memory address since I can change my XYPoint by changing myOrigin.  Is  there something I am doing wrong?

@interface
Code: (Objective-C)
#import <Foundation/Foundation.h>

@class XYPoint;
@interface Rectangle : NSObject

@property int width;
@property int height;
@property (strong, nonatomic) XYPoint *origin;

-(int) area;
-(int) perimeter;

//-(XYPoint *) origin;  // Getter method.  Returns the XYPoint.
//-(void) setOrigin: (XYPoint *) theOrigin;

@end

@implementation
Code: (Objective-C)
#import "Rectangle.h"
#import "XYPoint.h"

@implementation Rectangle
/*{
    XYPoint *origin;
}*/

@synthesize width;
@synthesize height;
@synthesize origin;

-(int) area
{
    return self.width * self.height;
}
-(int) perimeter
{
    return 2 * (self.width + self.height);
}

/*
-(XYPoint *) origin
{
    return origin;
}

-(void) setOrigin: (XYPoint *) theOrigin
{
   
    if (origin == nil)
    {
        origin = [[XYPoint alloc] init];
    }
   
    origin.x = theOrigin.x;
    origin.y = theOrigin.y;
}
*/
@end

main
Code: (Objective-C)
#import <Foundation/Foundation.h>
#import "Rectangle.h"
#import "XYPoint.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool
    {
        Rectangle *myRect = [[Rectangle alloc] init];
        XYPoint *myOrigin = [[XYPoint alloc] init];
                             
        myRect.width = 5;
        myRect.height = 10;
       
        myOrigin.x = 50;
        myOrigin.y = 100;
       
        myRect.origin = myOrigin;
       
        NSLog(@"The rectangles width is %i, and the height is %i", myRect.width, myRect.height);
        NSLog(@"The rectangles area is %i, and the perimeter is %i", [myRect area], [myRect perimeter]);
        NSLog(@"The origin is at (%i, %i)", myRect.origin.x, myRect.origin.y);
       
        myOrigin.x = 100;
        myOrigin.y = 200;
       
        NSLog(@"The origin is at (%i, %i)", myRect.origin.x, myRect.origin.y);
       
    }
    return 0;
}

Output
2012-04-29 19:00:21.079 Rectangles[39480:403] The origin is at (50, 100)
2012-04-29 19:00:21.081 Rectangles[39480:403] The origin is at (100, 200)

Take care,

Jon
Logged

Jon Thornham
rue
Jr. Member
**
Posts: 53






Reply #1 on: April 29, 2012, 11:12:21 PM

*origin is just a pointer (to a memory address).

myRect.origin = myOrigin; 

After execution of the above code, myRect.origin is pointing to the same memory address/location that is pointed by myOrigin.

Logged
jonthornham
Full Member
***
Posts: 169



WWW Email




Reply #2 on: April 29, 2012, 11:31:24 PM

Why are the factory methods designed this way?  Isn't this poor practice?  Stephan's examples lead me to believe this is true.

Take care,

Jon
Logged

Jon Thornham
rue
Jr. Member
**
Posts: 53






Reply #3 on: April 29, 2012, 11:47:50 PM

I'm not sure I get your point.

The book's example is this.. (which you commented out, and instead you used synthesize. )
Code: (Objective-C)
-(void) setOrigin: (XYPoint *) theOrigin 
{
     
    if (origin == nil)
    {
        origin = [[XYPoint alloc] init];
    }
     
    origin.x = theOrigin.x;
    origin.y = theOrigin.y;
}

This custom method (i.e. not synthesized) creates a separate copy of the origin point.
So the origin point of the Rectangle cannot be changed without using setOrigin.
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.