Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 23, 2009, 02:18 PM   #1
yaazz
macrumors newbie
 
Join Date: Jan 2009
Quick Question on Objective C protocols

Hey there, I am making a vector class in objective C and would like to make it as expandable as possible.
I would like to have a 2D version of the class, and a 3D version, and have the two classes able to work with each other interchangably. The only difference is that the 3d version will have functions to deal with cross products and stuff like that.

To do this, I made a protocol with all of the basic functions a vector would need. Then, I will just add additional classes into the interface for Vector3d.

But I can't figure out how to specify that I want the protocol to work with anything that implements the protocol.
I can explain better by just putting the protocol here
Code:
@protocol vecProt

+createAtX:Y:Z:;	//class variable to create a vector at <X,Y,Z>
+add:To:;			//class variable to add two vectors
+subtract:From:;	//class variable to subtract 2 vectors

-(id) init;				
-(id) add:;			//adds this vector to another
-(id) subtract:;		//subtracts this vector from another
-(id) dotProductWith:;//returns te dot product of this vector with another
-(id) length;		//Returns the length of the vector
-(id) lengthSquared;	//Returns the Squared Length of the vector
@end
right now I am returning id, but would like to say something along the lines of "return a vector", how can I do this exactly?

also, how do I specify what I can add and subtract from after the ":" in the method names?
yaazz is offline   0 Reply With Quote
Old Jan 23, 2009, 02:24 PM   #2
kpua
macrumors 6502
 
Join Date: Jul 2006
Code:
id <vecProt> aVector
is how you declare a variable conforming to the protocol.

So, your add method should look like this:

Code:
-(id) add:(id <vecProt>)other;
However, I don't think this is the best design in the world. In every implementation of these methods, you'll have to deal with what happens when you add a 2D vector to a 3D vector, or vice-versa.

(Also, unless you're using another object to represent length, (id) isn't the right return type for those methods)
kpua is offline   0 Reply With Quote
Old Jan 23, 2009, 03:11 PM   #3
yaazz
Thread Starter
macrumors newbie
 
Join Date: Jan 2009
Yea im not a fan either but there is extra credit for creating a vector2d class that interacts with vector 3d.
I suppose I could just derive vec3d from vec2d without the protocol
yaazz is offline   0 Reply With Quote
Old Jan 23, 2009, 05:04 PM   #4
kpua
macrumors 6502
 
Join Date: Jul 2006
That is an option, but then do you subclass vec3d when you need vec4d?

Why not just create a single N-dimensional vector class?
kpua is offline   0 Reply With Quote
Old Jan 24, 2009, 06:48 AM   #5
gnasher729
In Time-Out
 
Join Date: Nov 2005
Quote:
Originally Posted by yaazz View Post
Yea im not a fan either but there is extra credit for creating a vector2d class that interacts with vector 3d.
I suppose I could just derive vec3d from vec2d without the protocol
Protocols and subclasses should only be used in the right situations. You would use a protocol if the operations are actually the same. If one operation is adding a 2d vector, and another operation is adding a 3d vector, then they are not the same operation, and you shouldn't use a protocol.

Same with subclassing: A 3d vector is not a 2d vector. In C++, you can use templates to define multiple, similar classes, but not in Objective-C.

If this is for a programming class, you should demonstrate to your teacher that you learned one of the most important aspects of programming: When requirements are not clear, go back and check what the requirements are. So ask your teacher exactly what interaction between 2d and 3d she or he wants. It may make sense in some cases to treat a 2d vector as if it were a 3d vector with z = 0. In that case the easiest way is to duplicate the code that for example adds a 3d vector to a 3d vector and change it so it adds 2d to 3d, assuming the last coordinate is zero.

On the other hand, adding a method to the 2d vector class that returns a new 3d object with the same first two coordinates might be just as useful.
gnasher729 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Noob Objective-C / Framework / AVFoundation question Golgafrincham Mac Programming 0 Jan 30, 2014 03:54 AM
Objective-C / Java Question Mvkoe Mac Programming 3 Dec 8, 2012 09:27 AM
Objective C question farmerdoug Mac Programming 4 Sep 16, 2012 03:44 PM
A beginners question about protocols. ObjectiveV iPhone/iPad Programming 1 Jul 12, 2012 11:23 PM

Forum Jump

All times are GMT -5. The time now is 01:36 AM.

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

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