PDA

View Full Version : Strongly Typed NSArray




Fontano
Sep 25, 2008, 04:54 PM
My primarily background is the .NET enviornment... so please excuse me if I am not using the right term here.

In .NET, I could strongly type my arrays.

Example:
Instead of: Dim myObjects() as Object;
I could do: Dim myCarObjects() as Car;

This would give me enhanced supported in the development enviornment, directly from the array and I didn't have to CAST or connect the specific object to a specific defined object...

In my iPhone code so far, I have been using NSArray to hold my custom class objects, and then either defining a holding object of the specific type.. or casting it... primarily to avoid Compiler warning messages (as I don't like having any warnings, as a warning could be an error).

So instead of:
NSArray *_myObjects;
Is there a way to do
Car *_myCars(); or something like that (not sure how to express that since Car *_myCar is a singular definition).

I hope that made sense.



Fontano
Sep 25, 2008, 04:56 PM
Now that I typed that first message, and idea just popped into my head.

For each of my custom classes (that inherit from NSObject), do I have to build a custom class array (that inherits from NSArray) ?

If so, any good examples of doing that so that it is type specific and not just a new name for NSArray.

robbieduncan
Sep 25, 2008, 05:00 PM
Don't. NSArray/NSMutableArray are part of a class cluster. Subclassing is not really recommended.

mamcx
Sep 25, 2008, 05:15 PM
The point is, the "objective" side is decent. The "C" side sucks. like always do.

Dark day was the day that C was chosee as the standar.... how much time wasted!

Fontano
Sep 25, 2008, 05:20 PM
So from those replies, I am guessing I am stuck doing something like this:

NSArray *_myCars = [self.functionToBuildMyCars];
Car *wrkCar = [_myCars objectAtIndex:x];
[wkrCar startCar];

Instead of
[[_myCars objectAtIndex:x] startCar];

And not get any warnings (as I have been bit at times, and took a long time to debug when I ignore warnings)..

Oh well... worth a shot at asking... (also on larger classes, it is nice to have the prompting for method names).

robbieduncan
Sep 25, 2008, 05:24 PM
Or


[((Car *) [_myCars objectAtIndex:x]) startCar];

Oh and a matter of style: class names should be in start with a capital letter.

Fontano
Sep 25, 2008, 08:03 PM
Or


[((wrkCar *) [_myCars objectAtIndex:x]) startCar];

Oh and a matter of style: class names should be in start with a capital letter.

;)

wrkCar was my temporary object/variable.
Car was my custom class... ;)

Good reminder though on the uppercase.

robbieduncan
Sep 26, 2008, 03:30 AM
;)

wrkCar was my temporary object/variable.
Car was my custom class... ;)

Good reminder though on the uppercase.

Doh! Updated my code.