Reputation: 13860
I want to add NSArray with NSString values in it. I'm almost sure that I can use #define
for it, but I don't understand how to access and initialize that declared variables.
For example i have:
#define my_definition 0;
And i know how to access this 0 value. But how about NSString inside NSArray?
I have a definitions.h file. In there i have access from any my class.
Upvotes: 3
Views: 8476
Reputation: 17734
I came across someone else's code a few months ago that I adapted to my own purposes, mainly to save myself a lot of typing. I apologize in advance to the original author, I would love to give credit where it is due but I dug through my hard drive and couldn't find either the original samples or his or her name. If I do come across the original source I'll amend this post with the name of the original author.
At any rate, the basic idea is to use a $ character as your macro prefix character and then take variadic arguments. Yes, both gcc and Apple's LLVM compiler support this. I've gone absolutely crazy and converted nearly all the core types (arrays, strings, dates, dictionaries, etc.) based on a few examples, but here are some ideas to get you going:
#define $string( _format, _args... ) ( [NSString stringWithFormat: ( _format ), _args] )
#define $stringUTF8( _format, _args... ) ( [[NSString stringWithFormat: ( _format ), _args] UTF8String] )
// shorthand container creation (NSArray, NSMutableArray)
#define $array( _objs... ) ( { id objs[] = { _objs }; [NSArray arrayWithObjects: objs count: sizeof(objs) / sizeof(id)]; } )
#define $arrayM( _objs... ) ( { id objs[] = { _objs }; [NSMutableArray arrayWithObjects: objs count: sizeof(objs) / sizeof(id)]; } )
This lets you do some neat things like the following:
NSArray * array;
array = $array( @"foo", @"bar", $string( @"This is a %@ string", @"formatted" ) );
Saves a whole lot of typing!
Upvotes: 2
Reputation: 8808
Whoa... what are you trying to do here?
#define
is a preprocessor macro. The preprocessor knows nothing about Objective-C (or C for that matter... it's just a glorified text replacement engine), and Objective-C knows nothing about the preprocessor.
You almost certainly don't want a #define
for anything other than maybe numeric values (e.g. int
s) or an NSString
. (In practice, you will usually see extern NSString * ...
in lieu of #define
for a variety of reasons, too.)
If you need access to a particular NSArray
in an object, you can create an ivar that you populate in -init
or the like. If other classes need access to the array, make it a @property
or just declare a (public) method.
Maybe I'm misunderstanding what you are trying to do?
Upvotes: 5
Reputation: 40221
If you use #define, you define a preprocessor macro. It's NOT a global variable. Instead where ever you use that macro it will be replaced by what's after #define and only then will your code be compiled.
That being said, you can simply define an NSArray just as you would somewhere else:
#define kMyArray [NSArray arrayWithObjects:@"MyString1", @"MyString2", nil]
Notice, that I didn't end the line with a semicolon. Imagine if I now wrote somewhere in my code:
NSString *str1 = [kMyArray objectAtIndex:0];
This will be replaced by:
NSString *str1 = [[NSArray arrayWithObjects:@"MyString1", @"MyString2", nil] objectAtIndex:0];
If I had a semicolon in my definition, I would get a compiler error.
Upvotes: 4
Reputation: 29985
#define MY_DEFINITION @[ @"String1", @"String2" ]
Preferably without the semicolon
Upvotes: 0