objective-c 如何编写具有多个参数的方法/消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1066763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:49:09  来源:igfitidea点击:

How to write a method/message with multiple parameters?

objective-cparametersmethodsmessages

提问by Devoted

How do you write a method/message with multiple parameters?

您如何编写具有多个参数的方法/消息?

EDIT: Like multiple parameters for a single method/message, I mean.

编辑:就像单个方法/消息的多个参数一样,我的意思是。

回答by Jeff Hellman

You can write the declaration as such:

你可以这样写声明:

- (void) drawRoundedRect:(NSRect)aRect inView:(NSView *)aView withColor:(NSColor *)color fill:(BOOL)fill

The subsequent call (with 4 parameters) could look like:

随后的调用(带有 4 个参数)可能如下所示:

[self drawRoundedRect:rect inView:self withColor:[NSColor greenColor] fill:YES];

where rectis a previously defined NSRect, selfis the NSView the method is called from, an NSColorobject obtained from a nested method call, and the constant boolean value YES.

其中rect是先前定义的 NSRect,self是调用方法的 NSView,NSColor从嵌套方法调用中获得的对象,以及常量布尔值YES

回答by Quinn Taylor

In Objective-C, method names are properly called "selectors", and can be composed of one or more parts. If the method accepts one or more parameters, each part of the selector is of the form:

在 Objective-C 中,方法名称被恰当地称为“选择器”,并且可以由一个或多个部分组成。如果该方法接受一个或多个参数,则选择器的每一部分都采用以下形式:

selectorFragmentName:(ParameterType)parameterName

For example, you will see method declarations like this one from NSColor:

例如,您将在 NSColor 中看到类似这样的方法声明:

+ (NSColor*) colorWithDeviceRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha;

In this case, the method (selector) name is colorWithDeviceRed:green:blue:alpha:— the rest signifies the scope (- for instance method, + for class), return type (NSColor* here), and the type and name for each parameter.

在这种情况下,方法(选择器)名称是colorWithDeviceRed:green:blue:alpha:— 其余部分表示范围(- 例如方法,+ 表示类)、返回类型(此处为 NSColor*)以及类型和名称每个参数。

CRITICAL!Unlike most other languages, you cannot overload methods in Objective-C — this means you can't have two methods with the same selector but different number of parameters and/or order of types. For example, you can't have these two methods:

危急!与大多数其他语言不同,你不能在 Objective-C 中重载方法——这意味着你不能有两个具有相同选择器但参数数量和/或类型顺序不同的方法。例如,你不能有这两种方法:

- (id) initWithObjects:(NSArray*)anArray;
- (id) initWithObjects:(NSSet*)aSet;

Since the selector name for both is initWithObjects:Objective-C does not distinguish between the two. Selector names are translated into unique integers for extremely fast lookup, which is beneficial in the dynamic runtime, but a letdown for people that expect method overloading. The most common case for overloading in languages like Java is constructors, which is a non-issue in Objective-C because of the alloc/init pattern. For other methods, choosing unique names avoids the problem.

由于两者的选择器名称都是initWithObjects:Objective-C 不区分两者。选择器名称被转换为唯一的整数以实现极快的查找,这在动态运行时是有益的,但对于期望方法重载的人来说则令人失望。在 Java 等语言中,最常见的重载情况是构造函数,由于 alloc/init 模式,这在 Objective-C 中不是问题。对于其他方法,选择唯一名称可以避免该问题。

From a style standpoint, since the parameters are interspersed in the method selector, Objective-C programmers (and Xcode) will often align the parts of long selectors at the colon for readability, both for the declaration/definition:

从样式的角度来看,由于参数散布在方法选择器中,Objective-C 程序员(和 Xcode)通常会在冒号处对齐长选择器的部分以提高可读性,无论是声明/定义:

+ (NSColor*) colorWithDeviceRed:(CGFloat)red
                          green:(CGFloat)green
                           blue:(CGFloat)blue
                          alpha:(CGFloat)alpha;

and invocation:

和调用:

NSColor* myColor = [NSColor colorWithDeviceRed:0.5
                                         green:0.6
                                          blue:0.7
                                         alpha:0.9];

The whitespace is irrelevant to the compiler. If it makes it easier for you to read and understand, definitely use it.

空格与编译器无关。如果它使您更容易阅读和理解,请务必使用它。

回答by Peter N Lewis

Jeff accurately described what the methods look like. If you want to see how it would look as a C function, it would look something like:

杰夫准确地描述了这些方法的样子。如果你想看看它作为 C 函数的样子,它看起来像:

void drawRoundedRect_inView_withColor_fill( MyObject* self, SEL _cmd, NSRect aRect, NSView* aView, NSColor* color, BOOL fill );

The parameter "names" all join together to form a single method name, and two hidden parameters, self and _cmd are added to the front.

参数“names”全部连接在一起形成一个单一的方法名称,并在前面添加了两个隐藏参数,self 和_cmd。