Array of strings in C # define constant - objective c

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

For instance:

#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 an array of strings and the elements of this array.

I read (I don’t know if this is true) this way a new NSArray is created when it is used, I assume that the array is then released, so I think it's good because you only use this part of the memory when you need it necessary.

+3
source share
2 answers

I do not think you want to use #definefor this.

, . rows , NSArray . , KEY_ROWA , . , -

NSArray *columnas = rows;

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

. KEY_ROWA - objectAtIndex , .

, , , + - ( , ). :

Objective-C?

+6

, . , , ( ).

.m :

@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;
}

, , [MyClass mySingletonArray],

NSLog("%@", [[MyClass mySingletonArray] objectAtIndex:0]);
+7

All Articles