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

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 22, 2007, 02:26 AM   #1
Sancho
macrumors newbie
 
Join Date: Jan 2007
Dynamically typed arrays and non-object types

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

Last edited by Sancho; Jan 22, 2007 at 02:35 AM. Reason: correctness
Sancho is offline   0 Reply With Quote
Old Jan 22, 2007, 02:53 AM   #2
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: 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.
__________________
Go outside, the graphics are amazing!
HiRez is offline   0 Reply With Quote
Old Jan 22, 2007, 03:11 AM   #3
Soulstorm
macrumors 68000
 
Soulstorm's Avatar
 
Join Date: Feb 2005
I don't know if this will help you, but keep in mind that you can also use Objective C++.
Soulstorm is offline   0 Reply With Quote
Old Jan 22, 2007, 06:02 AM   #4
whooleytoo
macrumors 603
 
whooleytoo's Avatar
 
Join Date: Aug 2002
Location: Cork, Ireland.
Send a message via AIM to whooleytoo
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!
__________________
Mac <- Macintosh <- McIntosh apples <- John McIntosh <- McIntosh surname <- "Mac an toshach" <- "Son of the Chief"
whooleytoo is offline   0 Reply With Quote
Old Jan 22, 2007, 08:57 AM   #5
Nutter
macrumors 6502
 
Join Date: Mar 2005
Location: 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.
Nutter is offline   0 Reply With Quote
Old Jan 22, 2007, 09:35 AM   #6
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: Western US
Quote:
Originally Posted by Nutter View Post
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.
__________________
Go outside, the graphics are amazing!
HiRez is offline   0 Reply With Quote
Old Jan 22, 2007, 09:43 AM   #7
Nutter
macrumors 6502
 
Join Date: Mar 2005
Location: London, England
To be honest, I didn't know either until about an hour ago. Seriously useful.
Nutter is offline   0 Reply With Quote
Old Jan 22, 2007, 10:10 AM   #8
whooleytoo
macrumors 603
 
whooleytoo's Avatar
 
Join Date: Aug 2002
Location: Cork, Ireland.
Send a message via AIM to whooleytoo
Quote:
Originally Posted by Nutter View Post
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!
__________________
Mac <- Macintosh <- McIntosh apples <- John McIntosh <- McIntosh surname <- "Mac an toshach" <- "Son of the Chief"
whooleytoo is offline   0 Reply With Quote
Old Jan 22, 2007, 12:07 PM   #9
Sancho
Thread Starter
macrumors newbie
 
Join Date: Jan 2007
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++
Sancho is offline   0 Reply With Quote
Old Jan 22, 2007, 04:35 PM   #10
mduser63
macrumors 68040
 
mduser63's Avatar
 
Join Date: Nov 2004
Location: Salt Lake City, UT
Send a message via AIM to mduser63
Quote:
Originally Posted by Sancho View Post
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.
mduser63 is offline   0 Reply With Quote
Old Jan 23, 2007, 07:18 AM   #11
Soulstorm
macrumors 68000
 
Soulstorm's Avatar
 
Join Date: Feb 2005
Quote:
Originally Posted by Sancho View Post
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.
Soulstorm 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
Resolved: How do I convert an NSData object to an NSDictionary object, and vice versa? moonman239 iPhone/iPad Programming 6 May 2, 2014 01:17 PM
(Dynamically) Changing Application Windows Boquito17 Mac Applications and Mac App Store 0 Dec 17, 2013 02:09 PM
Mount network drives dynamically SnifferUK OS X 10.8 Mountain Lion 6 Dec 22, 2012 08:55 AM
Idea for adding values to screen dynamically. RookieAppler iPhone/iPad Programming 2 Nov 9, 2012 12:54 AM

Forum Jump

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

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

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