3

I need a array of strings in a constant. is a good idea to use #define?

For example:

#define rows [NSArray arrayWithObjects:  @"NameRowA",@"NameRowB",@"NameRowC", nil]


#define KEY_ROWA [columnas objectAtIndex:0]
#define KEY_ROWB [columnas objectAtIndex:1]
#define KEY_ROWC [columnas objectAtIndex:2]

I need to access to the array of strings and the elements of that array.

I have read, (i don´t know if is true) with this way it is created a new NSArray when it is used, I suppose then the array is released, so I think this is good because you only use that part of memory when you need it.

xarly
  • 2,054
  • 4
  • 24
  • 40
  • Can you review your code snippet, please. You don't use `rows` anywhere and what is `columnas`? – JeremyP May 09 '12 at 14:33
  • 1
    DO NOT program this way, anyone inheriting your code will likely attempt find your address and the names and numbers of the people who are closest to you. "I think this is good because you only use that part of memory when you need it." <- that is incorrect, #define is just a copy paste by the precompiler so putting this in the code where it belongs is the correct way. Check out these quesitons http://stackoverflow.com/questions/4715831/why-is-define-bad-and-what-is-the-proper-substitute, http://stackoverflow.com/questions/96196/when-are-c-macros-beneficial – Joe May 09 '12 at 14:36

2 Answers2

7

Use a singleton, it's just a couple of lines of code. All you need is a static singleton, and a static method for retrieving it (and settings it once).

Your .m file should contain this:

@implementation MyClass 

static NSArray *mySingletonArray; // this will be your array

+ (NSArray *)mySingletonArray // this is the static method for accessing your array
{
    if (nil == mySingletonArray) {
        mySingletonArray = [NSArray arrayWithObjects:@"firstString", @"secondString", nil];
    }

    return mySingletonArray;
}

Acquire what you need from you array using the static access method [MyClass mySingletonArray], e.g.

NSLog("%@", [[MyClass mySingletonArray] objectAtIndex:0]);
LenArt
  • 111
  • 1
  • 2
6

I don't think you want to use #define for this.

In your example, there is no constant array of strings made with this code. Every single time rows is used in your code, a new NSArray is going to be allocated. Also, KEY_ROWA refers to columnas, but that isn't in the rows define. I assume you have something like this

NSArray *columnas = rows;

There is really no difference between that and

NSArray *columnas = [NSArray arrayWithObjects: @"NameRowA",@"NameRowB",@"NameRowC", nil];

But the second line is a lot more obvious. The same is true with KEY_ROWA -- the objectAtIndex call would be more obvious and the macro doesn't get you anything.

I'm not sure what you need exactly, but if you need a shared constant array of strings inside of one class, you could declare it as + instead of - at the beginning, and allocate it once (this is a class variable as opposed to an instance variable). More info here:

How do I declare class-level properties in Objective-C?

Community
  • 1
  • 1
Lou Franco
  • 87,846
  • 14
  • 132
  • 192
  • Thank you for saving kante0 from such terrible coding practices. And thanks kante0 for asking first, generally if something about the way you are programming feels wrong, there is a good chance that it is. – Joe May 09 '12 at 14:43
  • @Lou Franco Thanks a lot for the advises. My intention is to create a const static array of strings I mean an array of string that nobody could modify. But I´d like when I ´m going to do: NSLog(@"all rows",rows); NSLog(@"KEY_ROWA %@", KEY_ROWA); NSLog(@"KEY_ROWB %@", KEY_ROWB); NSLog(@"KEY_ROWC %@", KEY_ROWC); For to first line I´ll have : An array and with all values (NameRowA, NameRowB, NameRowC) and with the rest the strings with every value. Thanks a lot, Rectify is of wise people :) P.D.: In the first code block I mean 'row' instead of 'columna'. – xarly May 10 '12 at 07:17
  • Each NSLog you wrote would have a totally different object. They would all be equivalent to each other, but they are not static. `rows` creates a new array every time you use it. – Lou Franco May 10 '12 at 16:23