PDA

View Full Version : Dynamically typed arrays and non-object types




Sancho
Jan 22, 2007, 02:26 AM
I'm new to objective-C programming and I have a few questions.

What is the proper way to create a dynamically typed array? I want objects that have an array member whose type is decided at the time of initialization of the object.

I'm used to being able to do this in C++ with template parameters: Vector<float, 3> obj; (actually this decides it at compile time, but the point is that I don't have to write a bunch of different vector classes; in Objective-C this is avoided by the dynamic run-time typing and I don't know how to take advantage of it in this case)

I'm using an NSMutableArray right now, but it requires objects, not objCTypes like int, float, etc. So, I have to go through the NSNumber object to add the elements to the array. Then, when I want to retrieve the elements, I want to retrieve them in the non-object type that the object was instantiated with (int, float, etc.), but I don't know how to record the underlying type to which they should be cast back to. Am I missing something? Am I thinking too much like c++? What is the proper solution to this problem?

Thanks,
Sancho



HiRez
Jan 22, 2007, 02:53 AM
If you want a generic object, create as an 'id' type instead of statically typing it, it can be whatever you want as long as it's an object. As far as storing primitive types within NSArrays, yes that can be a PITA, you must go through NSNumber or other object type in both directions. If you really need to know the original primitive type, I think the easiest way would be to create a custom object, a wrapper around NSNumber which stores the original type as an attribute (could just be an int enum code you have set up for the type), and use that object type in your array instead of NSNumber.

Soulstorm
Jan 22, 2007, 03:11 AM
I don't know if this will help you, but keep in mind that you can also use Objective C++. ;)

whooleytoo
Jan 22, 2007, 06:02 AM
I don't know of any easy way of doing what you want using Obj-C; you could either subclass NSNumber and add the code to note what type of number the NSNumber is being created with (int, float etc), or you could create a simple object which contains an NSNumber and another field to note the number type - this is probably less work.

Either way, the path of least work might be to follow Soulstorm's suggestion: use Obj-C++ and use C++ as you're accustomed to! ;)

Nutter
Jan 22, 2007, 08:57 AM
It's not easy to subclass NSNumber, as it's a class cluster.

I'd recommend that you use NSValue to store your objective C types in an NSArray. You can then use the methods -objCType and -getValue: to get the value out in the right data type.

HiRez
Jan 22, 2007, 09:35 AM
I'd recommend that you use NSValue to store your objective C types in an NSArray. You can then use the methods -objCType and -getValue: to get the value out in the right data type.Ah hah! I didn't even know NSValue kept track of the original type! Cool.

Nutter
Jan 22, 2007, 09:43 AM
To be honest, I didn't know either until about an hour ago. :) Seriously useful.

whooleytoo
Jan 22, 2007, 10:10 AM
It's not easy to subclass NSNumber, as it's a class cluster.

I'd recommend that you use NSValue to store your objective C types in an NSArray. You can then use the methods -objCType and -getValue: to get the value out in the right data type.

Cheers! That'll teach me to reply without checking the class documentation!

Sancho
Jan 22, 2007, 12:07 PM
Thanks for the suggestions all. I really like C++, but I'm trying to give 'pure' objective-C a full out chance at replacing C++ as my favorite language, so it was funny to hear the suggestions to just use C++ :)

mduser63
Jan 22, 2007, 04:35 PM
Thanks for the suggestions all. I really like C++, but I'm trying to give 'pure' objective-C a full out chance at replacing C++ as my favorite language, so it was funny to hear the suggestions to just use C++ :)

I think people suggested that not because C++ is necessarily the best way to do it, but just simply because it seemed like you're already comfortable with C++ and they were letting you know that you can mix the two. Anyway, NSValue certainly seems to be the way to go.

Soulstorm
Jan 23, 2007, 07:18 AM
Thanks for the suggestions all. I really like C++, but I'm trying to give 'pure' objective-C a full out chance at replacing C++ as my favorite language, so it was funny to hear the suggestions to just use C++ :)

No language can replace another. Objective C is not suitable for closed world applications. C++ is best for that. Objective C performs extremely well into open world applications.

Examples of closed and open world applications:
--Engine compartment of a car is closed world
--Passenger compartment of a car is open world.

(yes, this example is taken by Cocoa Programming book :))

For example. Working with NSSTrings is fabulous. But what this string type lacks, is functions for accessing individual characters inside an nsstring without calling a function. Operator overloading is missing (so, no <iostream>-like functions here) and you cannot ever,ever,ever make something like a stringstream. On the other hand, in C++, there is no ID object type. That little detail shows the things you can do with ObjC.

As you can see, there is no way one language can replace another. Your best bet is to use Objective C++ which marries elements from both worlds.