Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective-C2.0中的可选参数?_Objective C - Fatal编程技术网

Objective-C2.0中的可选参数?

Objective-C2.0中的可选参数?,objective-c,Objective C,在Objective-C2.0中,是否可以在参数可选的情况下创建一个方法?也就是说,您可以进行如下方法调用: [aFraction print]; 除此之外: [aFraction print: someParameter]; 在同一个节目中 苹果公司的《Objective-C2.0编程语言指南》将Obj-C与Python进行了对比,似乎认为这是不允许的。我还在学习,我想确定。如果可能,那么语法是什么,因为我的第二个代码示例不起作用 更新: 好的,我刚刚做了两个方法,都叫“print” 标题

在Objective-C2.0中,是否可以在参数可选的情况下创建一个方法?也就是说,您可以进行如下方法调用:

[aFraction print];
除此之外:

[aFraction print: someParameter];
在同一个节目中

苹果公司的《Objective-C2.0编程语言指南》将Obj-C与Python进行了对比,似乎认为这是不允许的。我还在学习,我想确定。如果可能,那么语法是什么,因为我的第二个代码示例不起作用

更新: 好的,我刚刚做了两个方法,都叫“print”

标题

实施

相关程序行


我真不敢相信这会奏效。有什么理由我不应该这样做吗?

您可以声明多个方法:

- (void)print;
- (void)printWithParameter:(id)parameter;
- (void)printWithParameter:(id)parameter andColor:(NSColor *)color;
在实现中,您可以执行以下操作:

- (void)print {
    [self printWithParameter:nil];
}

- (void)printWithParameter:(id)parameter {
    [self printWithParameter:nil andColor:[NSColor blackColor]];
}
编辑:

请不要同时使用
print
print:
。首先,它没有指明参数是什么,其次,没有人(ab)以这种方式使用Objective-C。大多数框架都有非常清晰的方法名,这是Objective-C编程如此轻松的原因之一。普通的开发人员不希望使用这种方法

有更好的名称,例如:

- (void)printUsingBold:(BOOL)bold;
- (void)printHavingAllThatCoolStuffTurnedOn:(BOOL)coolStuff;

在Objective-C中,您这样做是正确的。它在Cocoa中被广泛使用。例如,NSString的一些初始值设定项:

– initWithFormat:  
– initWithFormat:arguments:  
– initWithFormat:locale:  
– initWithFormat:locale:arguments:
它工作的原因是因为
是方法名称的一部分,因此就编译器而言,
print
print:
是完全不同的消息,它们之间的联系并不比“print”和“sprint”紧密


但是,您给出的方法的特定名称不是很好的例子,因为从名称上看不清楚参数是什么(或者如果参数是对象打印的,那么“打印”本身意味着什么)。比如说,
printFalseMessage
printMessageWithFlag:
稍微相关一点会更好,您可以使用可选参数。这意味着,如果愿意,您可以使用任意数量的参数调用方法。但这是C(varargs)的一个特性

例如,NSArray消息:

+ (id)arrayWithObjects:(id)firstObj, ...
用法示例:

NSArray *myArray;
NSDate *aDate = [NSDate distantFuture];
NSValue *aValue = [NSNumber numberWithInt:5];
NSString *aString = @"a string";

myArray = [NSArray arrayWithObjects:aDate, aValue, aString, nil];

这是直接从苹果的文档。使用nil

表示所有参数的结尾。多个可选参数的另一个选项是在NSDictionary中传递所有参数:

- (void)someMethodWithOptions:(NSDictionary *)options;
然后声明选项字典可能包含:

- key1, value must be a BlahObject
- key2, value must be a NonBlahObject
然后,您可以允许任何键不存在,甚至字典本身也可以为零。

为了避免解析问题:“aVariable”用作上一个参数的名称,而不是作为从编译器获得的选择器的一部分,作为警告,您应该执行以下操作:

- (void)printWithParameter:(BOOL)sv color:(NSColor *)color{
   // your cool code goes here
   if ( sv ) {
      NSLog(@"cool stuff turned on");
   }
   else {
      NSLog(@"cool stuff turned off");
   }
}
您可以调用该方法,例如:

[self printWithParameter:NO color:[UIColor someColor]] // NO instead of 0


当我将nil作为参数传递时,会收到一条警告:“传递'print:'的参数1将从指针生成整数,而不进行强制转换”。我应该忽略它吗?你能把这个错误发生的地方贴出来吗?我不确定你在做什么。不应发生此错误。如果参数类型为(id),则可以使用NIL。在我的代码中,参数类型是(BOOL)。解决方案是将NIL更改为0,因为这是预期的类型。我现在明白了。多方法很好。在您的特定情况下,使用NO而不是0。布尔值不应该是0或1,但不是,是的。哦,我明白了。因此,Obj-C中的“方法名”只是参数说明符的串联列表。使用它来表示可选参数而不是一个参数的可变长度列表将是非常反常的。如何声明方法的类型?+(id)arrayWithObjects:(NSArray*)参数@youshunei变量参数列表通过方法声明中的三个点(“…”)指定,并通过va_list、va_start()和va_end()命令(可能还有其他命令)访问。在Adam的示例中,firstObj实际上是一个独立于由…指定的变量参数列表的参数,并且参数列表以nil结尾是特定于NSArray对arrayWithObjects:方法的实现,而不是变量参数列表的一般规则。请参阅[NSString stringWithFormat:]以获取不需要nil值的示例。这在Ruby中非常常见。不过,在ObjC Cocoa中创建字典有点冗长,因此通常最好将其保存在大量相关选项中,而不是将其用作参数传递的“快捷方式”——这更像是一条风景优美的路线。你也会丢失类型检查。我认为苹果在一些方法中也使用了它,比如字符串。
- key1, value must be a BlahObject
- key2, value must be a NonBlahObject
- (void)printWithParameter:(BOOL)sv color:(NSColor *)color{
   // your cool code goes here
   if ( sv ) {
      NSLog(@"cool stuff turned on");
   }
   else {
      NSLog(@"cool stuff turned off");
   }
}
[self printWithParameter:NO color:[UIColor someColor]] // NO instead of 0
[self printWithParameter:YES color:[UIColor someColor]] // YES instead of 1