tags:

views:

274

answers:

3

I hope I'm wording my question properly, but what I'm trying to do is actually rather trivial.

I have a bunch of code with static object instantiation that looks like this:

Foo *aFoo1 = [[[Foo alloc] init] autorelease];
[anArray addObject:aFoo1];
Foo *aFoo2 = [[[Foo alloc] init] autorelease];
[anArray addObject:aFoo2];

I simply want to move all of that instantiation + population of anArray into a separate header file, and just include the header file where it's needed without the overhead of creating a new class to manage these instantiations. I just want to include the header file and get access to anArray once it's done adding all of the above objects and stuff it somewhere else. Is there anyway I can turn the above into a macro? Ideally I'll have all of this in a database soon, query the db, and instantiate the result set. Either way, I'm curious what the answer to this is in the format it's currently written in.

+2  A: 

My thought are to put the setup in a C function. You'd probably want to use two files:

//  Extra.h
// new file with descriptive header
NSArray* setupMyArray( ... );

-

//
// Extra.m
NSArray* setupMyArray( ... )
{
    NSMutableArray * anArray = [NSMutableArray arrayWithCapacity:0];
    // code to build array...
    //...
    return anArray;
}

Though I believe you'd be getting in to sketchy territory here. With Objective-C you are using an object orientated language. With C, you're not. It sounds like you want to build your objects in C and then use them in your class.

Frank V
Kind of. Your .h file would contain the function def. The implementation would go in a .c file.
Frank V
Don't think the compiler will take objective-c code in a .c file.
Coocoo4Cocoa
You can use a regular .m file. Objective-C inherits from C, so creating a regular "c-style" function in a .m file is perfectly valid.
e.James
@Frank: +1: You have the right answer here, and it's not sketchy territory at all. All you need to watch out for is proper memory management.
e.James
@Frank: I took the liberty of changing it for you. I hope you don't mind :)
e.James
@eJames: Thanks for the guidance. I wasn't quite sure about this as I had never done it. I did hear about it theoretically which is where I got the idea... Again, thank you.
Frank V
@eJames: Of course I don't mind. I think this is one of the strong points of stackoverflow. It helps prevent bad or misguided information from becoming popular...
Frank V
+2  A: 

You could use the following:

#define CREATE_INSTANCE_AND_ADD_TO_ARRAY(TYPE, NAME, ARRAY) \
        TYPE * NAME = [[[TYPE alloc] init] autorelease];    \
        [ARRAY addObject:NAME]

Called like so:

CREATE_INSTANCE_AND_ADD_TO_ARRAY(Foo, aFoo1, anArray);
CREATE_INSTANCE_AND_ADD_TO_ARRAY(Foo, aFoo2, anArray);
...
e.James
Plus, if you didn't care about the intermediate variables, couldn't you do something like `#define CREATE_INSTANCE_AND_ADD_TO_ARRAY(TYPE, ARRAY) [ARRAY addObject:[[[TYPE alloc] init] autorelease]]`
rampion
Good point. I just assumed that the intermediates were important to the code.
e.James
A: 

Check the text macros in Xcode under Edit > Text Macros > Objective-C.

(You can even assign them keyboard shortcuts in Xcode preferences)

jbrennan