Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Aug 21, 2010, 05: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, 06: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, 08: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, 09: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, 09: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, 09: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 offline   0 Reply With Quote
Old Aug 21, 2010, 09: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 10:21 PM. Reason: Posting code.
chown33 is offline   0 Reply With Quote
Old Aug 21, 2010, 10: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

Similar Threads
thread Thread Starter Forum Replies Last Post
Resale values. Southernboyj MacBook Pro 24 Oct 3, 2013 04:05 PM
5S Instant Re-sale Values rockitdog iPhone 63 Sep 17, 2013 11:25 PM
Method return values childoftheko4n iPhone/iPad Programming 8 Jul 11, 2013 08:55 AM
Resolved: Adding values from an NSArray to a UILabel fstigre iPhone/iPad Programming 11 Nov 13, 2012 03:40 PM
Viewing values in the debugger KarlJay iPhone/iPad Programming 19 Aug 9, 2012 07:26 PM

Forum Jump

All times are GMT -5. The time now is 04:08 PM.

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

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