如何在 Cocoa 中使用函数后声明该函数?

发布于 2024-07-12 23:34:48 字数 478 浏览 7 评论 0原文

我正在慢慢地将我的应用程序构建为工作状态。

我正在使用两个名为 setCollectionaddToCollection 的函数。 这些函数都接受 NSArray 作为输入。

我还有一个名为 add 的函数,我在其中使用了这两个函数。 当我尝试编译时,Xcode 显示错误:

“setCollection”未声明(在此函数中首次使用)

我想这与在活动函数下面定义的调用函数有关。 另一个猜测是这些函数应该全球化,以便在我的 add 函数中使用。

我通常是一名 php 编码员。 Php 处理这个问题的方式是第一种。 调用的函数应该位于使用它们的函数之前,否则它们就不存在。 有没有办法使函数在运行时仍然可用,或者我应该重新排列所有函数以使它们正常运行?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

他是夢罘是命 2024-07-19 23:34:48

您可以提前声明函数,如下所示:

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

//...

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

//...
强者自强 2024-07-19 23:34:48

如果您的函数是全局的(不是类的一部分),您只需将声明放在使用之前,就像 eJames 建议的那样。

如果您的函数实际上是方法(类的一部分),则必须在实现之前声明类的匿名类别,并将方法声明放在此接口中:

@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

这样,您不需要在 main 中公开您的函数MyClass 的接口。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文