Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Aug 21, 2010, 04:58 PM   #1
wiseone9
macrumors newbie
 
Join Date: Aug 2010
Help with values not being changed

hey there,
im very new to objective c programming, and i'm having trouble getting the values contained in a class to change. ill just go straight to the problem

so basically i created a class called vector that contains variables like this:
Code:
Vector.h:

@interface Vector : NSObject {
	float X;
	float Y;
	float magnitude;
	float direction;
}
@property float X;
@property float Y;
@property float magnitude;
@property float direction;


- (Vector *)setVector:(float *)newX : (float *)newY;

Vector.m:
#import "Vector.h"


@implementation Vector

	@synthesize X;
	@synthesize Y;
	@synthesize magnitude;
	@synthesize direction; //goes on for a while this is just the beginning
now, i have this function that is supposed to set a vector inside Vector.m. this is what it looks like:
Code:
- (Vector *) setVector:(float *)newX : (float *)newY{
	Vector *v1;
	v1.X = *newX;
	v1.Y = *newY;
	v1.magnitude = sqrtf(powf(X,2) + powf(Y, 2));
	v1.direction = atanf(Y/X);
	return v1;
}
but when i say
Code:
	vector1 = [vector1 setVector: &xfloat : &yfloat];
(xfloat and yfloat do have a value, i have confirmed this)

when i output vector1.X and vector1.Y i get 0 and 0...whats going on?
let me know if this is not clear
wiseone9 is offline   0 Reply With Quote
Old Aug 21, 2010, 05:02 PM   #2
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by wiseone9 View Post
Code:
- (Vector *) setVector:(float *)newX : (float *)newY{
	Vector *v1;
	v1.X = *newX;
	v1.Y = *newY;
	v1.magnitude = sqrtf(powf(X,2) + powf(Y, 2));
	v1.direction = atanf(Y/X);
	return v1;
}
At no point do you set v1 to anything: all you have done is declare a pointer to an object of type v1. That just reserves storage space for the pointer. It does not create a new object.

Also that method is very odd. Why does your set method (which would normally set the values of the existing method) return a new vector instead of setting the values in the instance the method is called on?
robbieduncan is offline   0 Reply With Quote
Old Aug 21, 2010, 07:03 PM   #3
wiseone9
Thread Starter
macrumors newbie
 
Join Date: Aug 2010
Quote:
Originally Posted by robbieduncan View Post
At no point do you set v1 to anything: all you have done is declare a pointer to an object of type v1. That just reserves storage space for the pointer. It does not create a new object.

Also that method is very odd. Why does your set method (which would normally set the values of the existing method) return a new vector instead of setting the values in the instance the method is called on?
Ok so if i read your post right, this is what you meant:
Code:
- (Vector *) setVector:(float *)newX : (float *)newY{
	
	X = *newX;
	Y = *newY;
	magnitude = sqrtf(powf(X,2) + powf(Y, 2));
	direction = atanf(Y/X);
        return self;
}
but it still does not work... i tried doing both:
Code:
vector1 = [vector1 setVector: &xfloat : &yfloat];
and just
Code:
[vector1 setVector: &xfloat : &yfloat];
wiseone9 is offline   0 Reply With Quote
Old Aug 21, 2010, 08:02 PM   #4
chown33
macrumors 603
 
Join Date: Aug 2009
How did you determine it didn't work? Debugger? NSLog? printf?

Post the code that shows whatever you did to determine it didn't work.

Debugging is mostly a process of gathering useful evidence. We need to see the evidence (i.e. the output) as well as the evidence-gatherer.
chown33 is offline   0 Reply With Quote
Old Aug 21, 2010, 08:19 PM   #5
wiseone9
Thread Starter
macrumors newbie
 
Join Date: Aug 2010
basically i just output it to a label that says "X coordinate: " and then a float value

when i output vector1.X, it always outputs 0, but if i output xfloat, which is what i tried to set vector1.X equal to, it outputs the proper number. (which is the number i enter in a text field above the label). therefore, i know that xfloat is being set to the correct number, but vector1.X just stays at 0
wiseone9 is offline   0 Reply With Quote
Old Aug 21, 2010, 08:29 PM   #6
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by wiseone9 View Post
Code:
- (Vector *) setVector:(float *)newX : (float *)newY{
	
	X = *newX;
	Y = *newY;
	...
float is a primitive. What is the need for all the screwy pointers?

Plus, setters normally just return void.

P.S. Have you instantiated your vector1 yet?
dejo is online now   0 Reply With Quote
Old Aug 21, 2010, 08:53 PM   #7
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by wiseone9 View Post
basically i just output it to a label that says "X coordinate: " and then a float value

when i output vector1.X, it always outputs 0, but if i output xfloat, which is what i tried to set vector1.X equal to, it outputs the proper number. (which is the number i enter in a text field above the label). therefore, i know that xfloat is being set to the correct number, but vector1.X just stays at 0
Post your code.

Describing code does nothing. We can't debug descriptions. Even if we could, the translation from description to code could be incorrect.

EDIT:
Test program and output is below.
Compiled as command-line tool for Mac OS 10.6.3.

Output:
Code:
2010-08-21 19:12:33.975 a.out[1847:903]  X:0.000000 Y:0.000000 mag:0.000000 dirn:0.000000
2010-08-21 19:12:33.979 a.out[1847:903]  X:12.300000 Y:12.400000 mag:17.465681 dirn:0.789447
vecmain.m:
Code:
#import <Foundation/Foundation.h>

#import "Vector.h"

static void
showVec( Vector * vec )
{
  NSLog( @" X:%f Y:%f mag:%f dirn:%f", vec.X, vec.Y, vec.magnitude, vec.direction );
}


int main (int argc, const char * argv[]) 
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

	Vector * vec1 = [[Vector alloc] init];  // initially empty
	
	showVec( vec1 );
	
	float x1 = 12.3f;
	float y1 = 12.4f;
	
	[vec1 setVector:&x1 :&y1];
	showVec( vec1 );
	
    [pool drain];
    return 0;
}
Vector.h:
Code:
#import <Foundation/Foundation.h>

@interface Vector : NSObject {
	float X;
	float Y;
	float magnitude;
	float direction;
}
@property float X;
@property float Y;
@property float magnitude;
@property float direction;


- (Vector *)setVector:(float *)newX : (float *)newY;

@end
Vector.m:
Code:
#import "Vector.h"

@implementation Vector

	@synthesize X;
	@synthesize Y;
	@synthesize magnitude;
	@synthesize direction; //goes on for a while this is just the beginning
	
	- (Vector *) setVector:(float *)newX : (float *)newY{
	X = *newX;
	Y = *newY;
	magnitude = sqrtf(powf(X,2) + powf(Y, 2));
	direction = atanf(Y/X);
	return self;
}

@end

Last edited by chown33; Aug 21, 2010 at 09:21 PM. Reason: Posting code.
chown33 is offline   0 Reply With Quote
Old Aug 21, 2010, 09:30 PM   #8
wiseone9
Thread Starter
macrumors newbie
 
Join Date: Aug 2010
thanks everyone

wow thanks for all the help everybody. it turns out that all my code is fine, i had just forgotten to instantiate vector1. (the ironic thing is that i had instantiated my vector2, but i never tested that one)

anyways, thanks and sorry for making such a big deal about a really dumb mistake
wiseone9 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Trying to record in garageband. Not working.. Help would be greatly appreciated SQUIJIT Digital Audio 5 Feb 6, 2014 04:46 PM
Help with port forwarding for Security software? PPFee Mac Applications and Mac App Store 1 Dec 11, 2011 03:34 PM
Help, total ram not being recognized!!! hitman10 Mac Pro 7 Feb 19, 2008 06:14 PM
Need help with HD not being recognied. TimJim Mac Peripherals 1 Oct 20, 2007 01:35 PM
HELP!! - Apple not being very helpful..... LooRoo General Mac Discussion 4 Nov 8, 2005 07:54 PM


All times are GMT -5. The time now is 06:48 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC