使用 NSArray 作为参数声明函数

发布于 2024-12-16 12:46:09 字数 1309 浏览 1 评论 0原文

我有一个函数如下:

-(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 技术交流群。

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

发布评论

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

评论(2

書生途 2024-12-23 12:46:09

请尝试以下操作:

-(int)ladderCalc:(NSArray*)amounts percentages:(NSArray*)percentages amount:(int)amount;

并更改实现的签名以匹配。然后,您就可以在方法的代码中引用带有名称的参数。即:金额、百分比和金额。

Try this instead:

-(int)ladderCalc:(NSArray*)amounts percentages:(NSArray*)percentages amount:(int)amount;

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.

清风无影 2024-12-23 12:46:09

正如@MarkGranoff 所说。

Objective-C 将其参数散布在方法名称中。

对于方法声明:

-(int)ladderCalcWithAmounts:(NSArray*)amounts percentages:(NSArray*)percentages amount:(int)amount;

方法名称是(冒号是名称的一部分):

ladderCalc:percentages:amount:

散布在方法名称中的参数是:

(NSArray*)amounts
(NSArray*)percentages
(int)amount;

这提高了“C”函数调用的可读性,这可能是:

int ladderCalcPercentagesAmount(NSArray *amounts, NSArray *percentages, amount);

从技术上讲,Objective-C 没有命名参数,而不是散布参数。命名参数往往意味着位置并不重要,关联的名称才重要,Python 的命名参数就是一个例子。

As @MarkGranoff says.

Objective-C has it's arguments interspersed in the method name.

For the method declaration:

-(int)ladderCalcWithAmounts:(NSArray*)amounts percentages:(NSArray*)percentages amount:(int)amount;

the method name is (the colons are part of the name):

ladderCalc:percentages:amount:

Interspersed in the method name the arguments are:

(NSArray*)amounts
(NSArray*)percentages
(int)amount;

This improved readability over a "C" function call which might be:

int ladderCalcPercentagesAmount(NSArray *amounts, NSArray *percentages, amount);

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.

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