The use of #define when creating a class

Discussion in 'iOS Programming' started by Hawkeye75, May 28, 2010.

  1. Hawkeye75 macrumors member

    Joined:
    Mar 29, 2010
    #1
    Hi folks,

    My new app will have 3 View Controllers and while I was building the one Controller I realized this would be a perfect opportunity to start flexing my newbie muscles. :)

    Since %90 percent of the methods could be left unchanged for the other two Controllers I thought I would just create a new View Controller class. My question is that I used something like...

    Code:
    #define numOfItems 3
    ...in my header so that I can keep the code as generic as possible. However are those preprocessor #define statements inherited? If so can they be overwritten? Because if I can just define new ones for other Controllers than all my code with work without me needing to make any changes.
     
  2. TiberiusXavier macrumors member

    Joined:
    Apr 18, 2010
    Location:
    Chicago
    #2
    #define values are only part of the preprocessor. They can be overwritten, but I am uncertain on your context.

    If that integral value is the only difference with you classes, why not make it a parameter?

    If the implementation is different, you can place the 90% common code in a base class and through inheritance make the custom controllers.
     
  3. Hawkeye75 thread starter macrumors member

    Joined:
    Mar 29, 2010
    #3
    I have lots of loops that scan through various arrays based on the number of items in the view. I designed it that way so I wouldn't have to modify the code if I just added another item later on.

    Are you just suggesting that I just make 'numOfItems' a property of the controller class and then initialize them separately for each controller? That wouldn't be much of a problem, I just like how nice and simple using #define can be when you know you are dealing with a fixed value.
     
  4. TiberiusXavier macrumors member

    Joined:
    Apr 18, 2010
    Location:
    Chicago
    #4
    #define is a weak way of controlling generic implementation. It is one thing to do conditional compiling for platform differences, debug .vs. release differences or for demo .vs. actual implementations. If you are using it as a parameter for instantiating the object, you are better off making it a class variable. Moreover, it avoids an accidental redefinition of the #defined value through multiple levels of inclusion.
     
  5. Hawkeye75 thread starter macrumors member

    Joined:
    Mar 29, 2010
    #5
    That does make sense, thanks for the advice. Once I implement the property it's nothing an easy search and replace can't fix.
     
  6. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    If this is meant to control looping through arrays it's better to either use an NSArray, which knows how many items are inside itself, or if you're using C style arrays to have the compiler calculate how many items are in the array for you.

    Maybe you should show an example of the kind of code you're talking about.
     
  7. Hawkeye75 thread starter macrumors member

    Joined:
    Mar 29, 2010
    #7
    Well I know I can use [myarray count] for loops but I also use it to initiate my arrays.

    Code:
    NSMutableArrray *myarray = [[NSMutableArray alloc] initWithCapacity:numOfItems];
    In my case the number of loops and the size of the arrays will always be the same as per my design. Needless to say, it's integral to several of my methods that the number is consistent through out. That's why I immediately thought of using the #define statement. However at the time I wasn't thinking how simply I can re-use the same code for my other two controllers. It won't be very difficult at all to just add 'numOfItems as an NSInteger class property.
     
  8. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #8
    Are you saying that the only reason you want this is for the capacity value? That value is not very useful.
     
  9. TiberiusXavier macrumors member

    Joined:
    Apr 18, 2010
    Location:
    Chicago
    #9
    PhoneyDeveloper has a point. If you are using this as range, you may be better off using the NSArray's count method. That would avoid two challenges: 1) keeping the length separately, 2) assuming the size of the array is larger or equal in size (so you don't do accessing getObjectAtIndex beyond your array).

    (I personally find C++ vectors easier)
     
  10. Hawkeye75 thread starter macrumors member

    Joined:
    Mar 29, 2010
    #10
    No, you misunderstand me...I was merely giving another example of where I use that number. I use it for looping, creating the array, assigning random values, etc... I literally use it everywhere in my application due to its symmetrical design. Personally I found it very useful. However, I'd like to point out the original intent of this thread was that I wanted to know how #define statements would behave when a subclass was created. As Tiberius already pointed out earlier in this thread it is much safer to implement a class property.
     

Share This Page