xcode 函数使用后如何在Cocoa中声明一个函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/483664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to declare a function in Cocoa after the function using it?
提问by xaddict
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.
我正在使用两个名为setCollection
and 的函数addToCollection
。这些函数都接受NSArray
作为输入。
I also have a function called add
in which I use both of these functions. When I try to compile, Xcode shows an error:
我还有一个被调用的函数add
,我在其中使用了这两个函数。当我尝试编译时,Xcode 显示错误:
'setCollection' undeclared (first use in this function)
'setCollection' 未声明(首次在此函数中使用)
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.
我想这与在活动函数下方定义的被调用函数有关。另一个猜测是这些函数应该被全球化以便在我的add
函数中可用。
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?
我通常是一个 php 编码员。Php 处理这个的方式是第一个。调用的函数应该在使用它们的函数之前,否则它们就不存在。有没有办法让函数在运行时仍然可用,或者我应该重新排列所有函数以使它们正常运行?
回答by e.James
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:
如果您正在创建自定义类,并且这些是成员函数(在 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];
//...
回答by mouviciel
If your functions are global (not part of a class), you just have to put the declaration before the use, just like eJames suggests.
如果你的函数是全局的(不是类的一部分),你只需要在使用之前放置声明,就像 eJames 建议的那样。
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
.
这样,您就不需要在MyClass
.