Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Sancho

macrumors newbie
Original poster
Jan 22, 2007
4
0
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

macrumors 603
Jan 6, 2004
6,250
2,576
Western US
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

macrumors 68000
Feb 1, 2005
1,887
1
I don't know if this will help you, but keep in mind that you can also use Objective C++. ;)
 

whooleytoo

macrumors 604
Aug 2, 2002
6,607
716
Cork, Ireland.
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

macrumors 6502
Mar 31, 2005
432
0
London, England
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

macrumors 603
Jan 6, 2004
6,250
2,576
Western US
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.
 

whooleytoo

macrumors 604
Aug 2, 2002
6,607
716
Cork, Ireland.
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

macrumors newbie
Original poster
Jan 22, 2007
4
0
Thanks

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

macrumors 68040
Nov 9, 2004
3,042
31
Salt Lake City, UT
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

macrumors 68000
Feb 1, 2005
1,887
1
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.