Strongly Typed NSArray

Discussion in 'iOS Programming' started by Fontano, Sep 25, 2008.

  1. Fontano macrumors member

    Joined:
    Jun 27, 2008
    #1
    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.
     
  2. Fontano thread starter macrumors member

    Joined:
    Jun 27, 2008
    #2
    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.
     
  3. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    Don't. NSArray/NSMutableArray are part of a class cluster. Subclassing is not really recommended.
     
  4. mamcx macrumors regular

    Joined:
    Mar 13, 2008
    #4
    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!
     
  5. Fontano thread starter macrumors member

    Joined:
    Jun 27, 2008
    #5
    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).
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    Or

    Code:
    [((Car *) [_myCars objectAtIndex:x]) startCar];
    Oh and a matter of style: class names should be in start with a capital letter.
     
  7. Fontano thread starter macrumors member

    Joined:
    Jun 27, 2008
    #7
    ;)

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

    Good reminder though on the uppercase.
     
  8. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    Doh! Updated my code.
     

Share This Page