gpt4 book ai didi

objective-c - Objective C. 奇怪的语法

转载 作者:太空狗 更新时间:2023-10-30 03:29:16 25 4
gpt4 key购买 nike

我发现了一种在 Objective C 中声明方法的奇怪方式。

.h文件中的方法声明:

-(void)methodName:(NSString *)str, int i;

.m文件中的方法实现:

-(void)methodName:(NSString *)str, int i
{
NSLog(@"str = %@, int = %d", str, i);
}

我可以这样调用这个方法:

[self methodName:@"stringExample", 99];

而且它会很好地工作。

我的问题是什么时候应该使用这种语法。和通常的申报有什么区别吗?

最佳答案

如所述here ,这些参数是可选的:

Methods that take a variable number of parameters are also possible, though they’re somewhat rare. Extra parameters are separated by commas after the end of the method name. (Unlike colons, the commas are not considered part of the name.) In the following example, the imaginary makeGroup: method is passed one required parameter (group) and three parameters that are optional:

[receiver makeGroup:group, memberOne, memberTwo, memberThree];

所以是的,声明与通常的声明不同。除了 varargs 方法,我找不到这种类型声明的任何常规用法,其中可选参数声明为 ...

关于objective-c - Objective C. 奇怪的语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11774082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com