如何在 Cocoa 中使用函数后声明该函数?
我正在慢慢地将我的应用程序构建为工作状态。
我正在使用两个名为 setCollection
和 addToCollection
的函数。 这些函数都接受 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以提前声明函数,如下所示:
如果您正在创建自定义类,并且这些是成员函数(通常在 Objective-C 中称为方法),那么您可以在类头中声明这些方法,并在类源文件中定义它们:
You can declare functions ahead of time as follows:
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:
如果您的函数是全局的(不是类的一部分),您只需将声明放在使用之前,就像 eJames 建议的那样。
如果您的函数实际上是方法(类的一部分),则必须在实现之前声明类的匿名类别,并将方法声明放在此接口中:
这样,您不需要在 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:
This way, you don't need to expose your functions in the main interface of
MyClass
.