|Jan 23, 2009, 01:18 PM||#1|
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
@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
also, how do I specify what I can add and subtract from after the ":" in the method names?
|Jan 23, 2009, 01:24 PM||#2|
id <vecProt> aVector
So, your add method should look like this:
-(id) add:(id <vecProt>)other;
(Also, unless you're using another object to represent length, (id) isn't the right return type for those methods)
|Jan 23, 2009, 02:11 PM||#3|
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
|Jan 24, 2009, 05:48 AM||#5|
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.
|Thread Tools||Search this Thread|
|thread||Thread Starter||Forum||Replies||Last Post|
|Noob Objective-C / Framework / AVFoundation question||Golgafrincham||Mac Programming||0||Jan 30, 2014 02:54 AM|
|Objective-C / Java Question||Mvkoe||Mac Programming||3||Dec 8, 2012 08:27 AM|
|Objective C question||farmerdoug||Mac Programming||4||Sep 16, 2012 02:44 PM|
|A beginners question about protocols.||ObjectiveV||iPhone/iPad Programming||1||Jul 12, 2012 10:23 PM|
All times are GMT -5. The time now is 04:57 AM.