How to declare a function in Cocoa after using this function?

I am slowly building my application up and running.

I use two functions called setCollectionand addToCollection. These functions are taken NSArrayas 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' uneclared (use in this function first)

I assume this is due to a function called active below. Another suggestion would be that functions should be globalized for use inside my function add.

Usually I am a php encoder. The way Php handles is the first. Called functions must be in front of functions that use them, because otherwise they simply do not exist. Is there a way to make functions still available at runtime, or do I need to change all the functions so that they work correctly?

+3
source share
2 answers

If your functions are global (not part of the class), you just need to put an ad before use, as eJames suggests.

If your functions are actually methods (part of a class), you must declare an anonymous category of your class before implementation and place declarations of this method 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

, MyClass.

+5

:

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
}

, - ( Objective-C), :

//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];

//...
+8

Source: https://habr.com/ru/post/1702304/


All Articles