Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-25-2010, 12:05 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 12
Unicyclist is on a distinguished road
Smile Don't understand why I can't set my values

Hi!

I'm pretty new to programming and I'm working through Programming in Objective-C by Stephen Kochan.

I've got a problem I can't seem to figure out.

Its an exercise in the book where we're supposed to create a program to work with rectangles. I've included the details below.

The problem is mainly in "Rectangle.m"
I don't understand why i can't seem to use the method "intersect" to set
Code:
 new.origin.x
and
Code:
 new.origin.y
I think its related to how I defined "setOrigin" but I don't understand how it all comes together.
I noticed that I can't simply use
Code:
 origin.x = value
and
Code:
 origin.y = value
and must instead use
Code:
 [origin setX: value andY: value]
.

I think they're related but I'm not sure. Thanks in advance!

this is the file XYPoint.h
Code:
#import <Foundation/Foundation.h>

@interface XYPoint : NSObject 
{
	float x;
	float y;
}

@property float x, y;

-(void) setX: (float) xVal andY: (float) yVal;
@end
this is the file XYPoint.m
Code:
#import "XYPoint.h"

@implementation XYPoint

@synthesize x, y;

-(void) setX: (float) xVal andY: (float) yVal;
{
	x = xVal;
	y = yVal;
}

@end
this is the file Rectangle.h
Code:
#import <Foundation/Foundation.h>
#import "XYPoint.h"

@interface Rectangle : GraphicObject
{
	float width;
	float height;
	XYPoint *origin;
}

@property float width, height;

-(XYPoint *) origin;
-(void) setOrigin: (XYPoint *) pt;
-(void) set: (float) w : (float) h;
-(float) area;
-(float) perimeter;
-(void) translate: (XYPoint *) trans;
-(Rectangle *) intersect: (Rectangle *) second;

@end
this is the file Rectangle.m
Code:
#import "Rectangle.h"

@implementation Rectangle

@synthesize width, height;

-(void) set: (float) w : (float) h
{
	width = w;
	height = h;
}

-(void) setOrigin: (XYPoint *) pt
{
	origin = [[XYPoint alloc] init];
	[origin setX: pt.x andY: pt.y];
}

-(float) area
{
	return width * height;
}

-(float) perimeter
{
	return (width + height) * 2;
}

-(XYPoint *) origin
{
	return origin;
}

-(void) translate: (XYPoint *) trans
{
	origin.x += trans.x;
	origin.y += trans.y;
}

-(Rectangle *) intersect: (Rectangle *) second
{	
	Rectangle * new = [[Rectangle alloc] init];
	
	new.width = (origin.x + width) - second.origin.x;
	new.height = (second.origin.y + second.height) - origin.y;
	
	new.origin.x = 4;
	new.origin.y = 5;
		
	return new;
}

Last edited by Unicyclist; 03-25-2010 at 12:22 AM.
Unicyclist is offline   Reply With Quote
Old 03-25-2010, 12:48 AM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Where's the init method for your Rectangle class? I'm gonna guess that you never set "origin" and it's still pointing to nil. Any message you send to nil will do nothing and return zero.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 03-25-2010, 07:43 AM   #3 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 12
Unicyclist is on a distinguished road
Default Thanks!

Hi smasher!

Thank you for the prompt response... I'm not sure what you're referring to exactly but when I compile the program and let it run it does so fine except for the intersect method.

Also, would you happen to know why I can't seem to define origin.x using origin.x = value instead of resorting to [origin setX: value andY: value]?

Thanks!
Unicyclist is offline   Reply With Quote
Old 03-25-2010, 11:35 AM   #4 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

You did not say what goes wrong when you set origin.x = value. Do you get a compiler error, or a crash, or does it fail to store the value and origin.x always returns zero?

If it's the last case -- origin.x always returns zero -- then it's because origin does not point to a valid XYPoint. If you never set it then it still points to nil, or zero. Checking nil.x or nil.y will always return zero.

You should point origin at a valid point, and the right place to do that is probably in the init method for your Rectangle class.
__________________

Free Games!

Last edited by smasher; 03-25-2010 at 12:49 PM.
smasher is offline   Reply With Quote
Old 03-25-2010, 12:43 PM   #5 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 12
Unicyclist is on a distinguished road
Default Thanks!

Thanks Smasher!

I understand now. It makes a lot of sense =)

Cheers!
Unicyclist
Unicyclist is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 460
16 members and 444 guests
7twenty7, AlanFloyd, David-T, iAppDeveloper, imac74, Jaxen66, logan, lovoyl, markuschow, Music Man, mutantskin, Sami Gh, SLIC, solardrift, unicornleo, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,683
Threads: 94,131
Posts: 402,932
Top Poster: BrianSlick (7,990)
Welcome to our newest member, unicornleo
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:08 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0