使用 NSArray 作为参数声明函数
我有一个函数如下:
-(int)ladderCalc:(NSArray*)amounts percentages:(NSArray*)percentages amount:(int)amount
{
// Do some stuff
return foo;
}
我在头文件中声明了这样的:
-(int)ladderCalc:(NSArray*)amounts percentages:(NSArray*)percentages amount:(int)amount;
但是当我尝试使用同一文件中其他地方返回的 int 值时,出现错误“函数的隐式声明在 c99 中无效”。我没有正确声明该功能吗?
更新
我意识到我没有以标准化的方式声明这一点,我将我的声明更改为 MarkGranoff 的建议(请参阅上面的更改),但这次我仍然收到它作为警告。
以下是我如何调用此函数的上下文:
-(int)fooTotal: (int)amount
{
int totalFee = 0;
// Declare arguments
NSArray *percentages = [[NSArray alloc] initWithObjects:firstValue, secondValue, thirdValue, fourthValue, fifthValue, nil];
NSArray *amounts = [[NSArray alloc] initWithObjects:sixthValue, seventhValue, eigthValue, ninthValue, nil];
totalFee = ladderCalc(amounts,percentages,amount);
return totalFee;
}
因此,尽管就 Obj-C 风格而言这似乎有意义,但我仍然收到警告。
我很确定我没有正确调用这个函数,当我编译项目时,我收到了无法识别的符号错误。
Undefined symbols for architecture i386:
"_ladderCalc", referenced from:
-[FeeCalcLibrary getMFModelTotal:] in FeeCalcLibrary-A83D2A7637F57664.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have a function as follows:
-(int)ladderCalc:(NSArray*)amounts percentages:(NSArray*)percentages amount:(int)amount
{
// Do some stuff
return foo;
}
I have declared like this in the header file:
-(int)ladderCalc:(NSArray*)amounts percentages:(NSArray*)percentages amount:(int)amount;
But I am getting an error "implicit declaration of function is invalid in c99" when I try to use the int value returned elsewhere in the same file. Am I not declaring the function correctly?
UPDATE
I am realizing that I am not declaring this in the standardized way, I changed my declaration to MarkGranoff's recommendation (see the changes above) but I am still getting it as a warning this time.
Here is the context of how I am calling this function:
-(int)fooTotal: (int)amount
{
int totalFee = 0;
// Declare arguments
NSArray *percentages = [[NSArray alloc] initWithObjects:firstValue, secondValue, thirdValue, fourthValue, fifthValue, nil];
NSArray *amounts = [[NSArray alloc] initWithObjects:sixthValue, seventhValue, eigthValue, ninthValue, nil];
totalFee = ladderCalc(amounts,percentages,amount);
return totalFee;
}
So, I am still getting a warning even though this seems to make sense as far as Obj-C style is concerned.
I am pretty sure I am not calling this function correctly, I am getting an unrecognized symbol error when I compile the project.
Undefined symbols for architecture i386:
"_ladderCalc", referenced from:
-[FeeCalcLibrary getMFModelTotal:] in FeeCalcLibrary-A83D2A7637F57664.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请尝试以下操作:
并更改实现的签名以匹配。然后,您就可以在方法的代码中引用带有名称的参数。即:金额、百分比和金额。
Try this instead:
and change the signature of the implementation to match. Then you have arguments with names you can reference in the code of the method. Namely: amounts, percentages, and amount.
正如@MarkGranoff 所说。
Objective-C 将其参数散布在方法名称中。
对于方法声明:
方法名称是(冒号是名称的一部分):
散布在方法名称中的参数是:
这提高了“C”函数调用的可读性,这可能是:
从技术上讲,Objective-C 没有命名参数,而不是散布参数。命名参数往往意味着位置并不重要,关联的名称才重要,Python 的命名参数就是一个例子。
As @MarkGranoff says.
Objective-C has it's arguments interspersed in the method name.
For the method declaration:
the method name is (the colons are part of the name):
Interspersed in the method name the arguments are:
This improved readability over a "C" function call which might be:
Technically, Objective-C does not have named parameters, rather interspersed parameters. Named parameters tends to imply that position is not important just the associated names, an example are Python's named parameters.