Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mymac1

macrumors newbie
Original poster
Jul 28, 2009
20
0
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
 

ulbador

macrumors 68000
Feb 11, 2010
1,554
0
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
 

bredell

macrumors regular
Mar 30, 2008
127
1
Uppsala, Sweden
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.
 

mymac1

macrumors newbie
Original poster
Jul 28, 2009
20
0
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).
 

bredell

macrumors regular
Mar 30, 2008
127
1
Uppsala, Sweden
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).

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.
 

mymac1

macrumors newbie
Original poster
Jul 28, 2009
20
0
Probably doesn't apply to the OP's case, but there are times when a singleton is preferred to global variables.

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,
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.