Basics of methods and variable definitions.

Discussion in 'iOS Programming' started by mymac1, Apr 27, 2010.

  1. mymac1 macrumors newbie

    Joined:
    Jul 28, 2009
    #1
    Hi,

    I have been defining variables within methods, an example would look something like:
    Code:
    -(void)my_method{
    const int arrayofints[]={1,2,3,4,5,6,};
    
    /*other stuff */
    
    }
    I want to use this arrayofints outside the method, but there are errors when I put it anywhere else, partly because I am setting the variable.
    I also tried creating a separate .h file (and even a .m) file as I would like to store the data separate and be acessible to other objects & methods within my program, but still getting errors.

    Where would be the best place to locate such definitions?

    Also curious regarding the use of a method as above, would this mean that each time the method is called, the definitions are redefined?

    Thanks in advance
    m1
     
  2. ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #2
    That would just declare a variable that is local to that method.

    If you want to make a class variable, you have to put it in the .h file (just the definition, not the values) and then define the values either in the init methods or something like that.

    If you want to make those class variables public (meaning you can access them from other classes), you have to make it a @property. If you want to auto generate the getter/setter methods, you have to use the @synthesize keyword
     
  3. bredell macrumors regular

    Joined:
    Mar 30, 2008
    Location:
    Uppsala, Sweden
    #3
    You can make it static:

    Code:
    @implementation FooBar
    
    static const int arrayofints[]={1,2,3,4,5,6};
    
    -(void)my_method {
    
    }
    Remember that static variables are shared with all instances. This is not a problem if you're using them to hold constant values, but if one instance should change the value of a static variable it changes in all other instances as well. Which can be quite handy sometimes.
     
  4. mymac1 thread starter macrumors newbie

    Joined:
    Jul 28, 2009
    #4
    Hi,
    Making it static seems to work, in addition I have to place the code below the implementation within my main .m file.

    Is it possible to create a separate #import file, if so what would the extension type be .m or .h (or do I need both).
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Sounds like what you really want is a singleton.
     
  6. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #6
    A global variable is a compiler created singleton.

    Very low overhead.
     
  7. bredell macrumors regular

    Joined:
    Mar 30, 2008
    Location:
    Uppsala, Sweden
    #7
    Yes, you can create a new header file for your data. Just create it, it should be a .h file, and include it from your .m file.
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Probably doesn't apply to the OP's case, but there are times when a singleton is preferred to global variables.
     
  9. mymac1 thread starter macrumors newbie

    Joined:
    Jul 28, 2009
    #9
    Thanks your your help guys!
    What I'd been doing was adding a new file from within the XCODE which created a .m and .h with some presets, this was causing me the problem.
    I just created a new txt file and renamed it with a .h extension, threw in my consts and added that to the list. seems to be working ok (for the moment :D)

    I looked up the idea of using a singleton and it seems like that would be a good idea, something for future reference definitely.

    Thanks again,
     

Share This Page