views:

3999

answers:

2

I'm slowly building my application to a working state.

I'm using two functions called setCollection and addToCollection. These functions both accept NSArray as input.

I also have a function called add in which I use both of these functions. When I try to compile, Xcode shows an error:

'setCollection' undeclared (first use in this function)

I guess this has to do with the function called being defined below the active function. Another guess would be that the functions should be globalized to be useable inside my add function.

I'm normally a php coder. the way Php handles this is the first one. The functions called should be before the functions using them, because otherwise they just don't exist. Is there a way to make functions still to come available at runtime, or should I rearrange all functions to make them function properly?

+3  A: 

you can declare functions ahead of time as follows:

void setCollection(NSArray * array);
void addToCollection(NSArray * array);

//...

// code that calls setCollection or addToCollection

//...

void setCollection(NSArray * array)
{
    // your code here
}

void addToCollection(NSArray * array)
{
    // your code here
}

If you are creating a custom class, and these are member functions (usually called methods in objective-c) then you would declare the methods in your class header and define them in your class source file:

//MyClass.h:
@interface MyClass : NSObject
{

}

- (void)setCollection:(NSArray *)array;
- (void)addToCollection:(NSArray *)array;

@end


//MyClass.m:

#import "MyClass.h"

@implementation MyClass

- (void)setCollection:(NSArray *)array
{
    // your code here
}

- (void)addToCollection:(NSArray *)array
{
    // your code here
}

@end


//some other source file

#import "MyClass.h"

//...

MyClass * collection = [[MyClass alloc] init];
[collection setCollection:someArray];
[collection addToCollection:someArray];

//...
e.James
thanks. this was very helpful! together with the answer below (i Was using the function as accesors, referenceing them as - (void)) this actually got my application to work partly as I wanted.
xaddict
@(whoever down-voted this): can you please tell me what was wrong with it? Thanks.
e.James
+3  A: 

If your functions are global (not part of a class), you just have to put the declaration before the use, just like eJames suggests.

If your functions actually are methods (part of a class), you have to declare an anonymous category of your class before the implementation and put your method declarations in this interface:

@interface Myclass()
- (void) setCollection:(NSArray*)array;
- (void) addToCollection:(NSArray*)array;
@end

@implementation Myclass

// Code that calls setCollection or addToCollection

- (void) setCollection:(NSArray*)array
{
    // your code here
}

- (void) addToCollection:(NSArray*)array
{
    // your code here
}

@end

This way, you don't need to expose your functions in the main interface of MyClass.

mouviciel
Aas a follow-up to this question, do you know a site with beginner tutorials about how to link the array of arrays to a tableview inside IB? please PM me or something if so or if you personally know how to do this. Thanks!
xaddict
I didn't check for your follow-up question, but a nice site with cocoa tutorials is http://cocoadevcentral.com/
mouviciel