objective-c Swift References 中的 _ 下划线代表什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24437388/
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:21:43  来源:igfitidea点击:

What's the _ underscore representative of in Swift References?

objective-cfunctionswiftdeclaration

提问by Confused

In the reference section of Apple's docs there's lots of instances of this sort of thing:

在 Apple 文档的参考部分中,有很多此类事情的实例:

func runAction(_action: SKAction!)

func runAction(_action: SKAction!)

The Objective-C 'equivalent' of this is:

Objective-C 的“等价物”是:

- (void)runAction:(SKAction *)action

- (void)runAction:(SKAction *)action

It strikes me that it's probably important that (in the Swift reference) there's a space after the underscore and "action" is written in italics.

我觉得很重要的是(在 Swift 参考中)下划线后面有一个空格,“动作”用斜体写成。

But I can't figure out what this is trying to convey. So perhaps the question is... is there a reference for the conventions used in the references?

但我无法弄清楚这试图传达什么。所以也许问题是......是否有参考文献中使用的约定的参考?

-- here's the page I'm referencing in this reference to the underscore use: https://developer.apple.com/documentation/spritekit/sknode#//apple_ref/occ/instm/SKNode/runAction

- 这是我在此参考中引用的下划线使用页面:https: //developer.apple.com/documentation/spritekit/sknode#//apple_ref/occ/instm/SKNode/runAction

Update

更新

Swift 3 has made some changes to how function/method parameter names and argument labels are used and named. This has ramifications on this question and its answer. @Rickster does an amazing job of answering a different question about _underscores in functions that clears much of this up, here: Why do I need underscores in swift?

Swift 3 对函数/方法参数名称和参数标签的使用和命名方式进行了一些更改。这对这个问题及其答案有影响。@Rickster 在回答有关 _underscores 函数中的一个不同问题方面做得非常出色,这些问题清除了大部分内容,在这里:为什么我需要快速使用下划线?

采纳答案by Aaron He

Both answers were correct but I want to clarify a little bit more.

两个答案都是正确的,但我想再澄清一点。

_is used to modify external parameter name behavior for methods.

_用于修改方法的外部参数名称行为

In Local and External Parameter Names for Methodssection of the documentation, it says:

在文档的方法部分的本地和外部参数名称中,它说:

Swift gives the first parameter name in a methoda local parameter name by default, and gives the second and subsequent parameter names both local and external parameter namesby default.

Swift默认给方法中的第一个参数名称一个本地参数名称,默认给第二个和后续参数名称本地和外部参数名称

On the other hand, functions by default don't have external parameter names.

另一方面,默认情况下函数没有外部参数名称。

For example, we have this foo()method defined in class Bar:

例如,我们foo()在 class 中定义了这个方法Bar

class Bar{
    func foo(s1: String, s2: String) -> String {
        return s1 + s2;
    }
}

When you call foo(), it is called like bar.foo("Hello", s2: "World").

当你打电话时foo(),它被称为 like bar.foo("Hello", s2: "World")

But, you can override this behavior by using _in front of s2where it's declared.

但是,您可以通过_s2声明的位置前面使用来覆盖此行为。

func foo(s1: String, _ s2: String) -> String{
    return s1 + s2;
}

Then, when you call foo, it could be simply called like bar.foo("Hello", "World")without the name of the second parameter.

然后,当您调用 时foo,可以像bar.foo("Hello", "World")没有第二个参数的名称一样简单地调用它。

Back to your case, runActionis a method because it's associated with type SKNode, obviously. Thus, putting a _before parameter actionallows you to call runActionwithout an external name.

回到你的情况,runAction是一种方法,因为它SKNode显然与 type 相关联。因此,放置一个_before 参数action允许您在runAction没有外部名称的情况下进行调用。

Update for Swift 2.0

Swift 2.0 更新

Function and method now work the same wayin terms of local and external argument name declaration.

函数和方法现在在本地和外部参数名称声明方面的工作方式相同

Functions are now called by using external parameter name by default, starting at 2nd parameter. This rule only applies to pure Swift code.

现在默认使用外部参数名称调用函数,从第二个参数开始。此规则仅适用于纯 Swift 代码。

So, by providing an _in front of a function, the caller won't have to specify external parameter name, just like what you would do for a method.

因此,通过_函数前面提供,调用者不必指定外部参数名称,就像您对方法所做的那样。

回答by David Berry

The underscore is a general token used to indicate a discarded value.

下划线是用于指示丢弃值的通用标记。

In this specific case, it means that the function will be invoked as runAction(argument)instead of runAction(action:argument)

在这种特定情况下,这意味着该函数将被调用为runAction(argument)而不是runAction(action:argument)

In other contexts it has other similar meaning, e.g. in:

在其他情况下,它具有其他类似的含义,例如:

for _ in 0..<5 { ... }

It means that we merely want to execute the block 5 times and we don't care about the index within the block.

这意味着我们只想执行块 5 次,而不关心块内的索引。

In this context:

在这种情况下:

let (result, _) = someFunctionThatReturnsATuple()

It means that we don't care what the second element of the tuple is, only the first.

这意味着我们不关心元组的第二个元素是什么,只关心第一个元素。

回答by Jakub Truhlá?

Since Swift 3all argument labels are required by default.

从 Swift 3 开始默认情况下所有参数标签都是必需的。

You can force an IDE to hide an argument label with _.

您可以强制 IDE 隐藏带有_.

func foo(a: String) {
}

func foo2(_ a: String) {
}

called foo(a: "abc")and foo2("abc")

被称为foo(a: "abc")foo2("abc")

Note:This can be used only when ais the (external) argument labeland (internal) variable nameat the same time. It's equivalent - func foo(a a: String)won't accept the _.

注意:这只能当使用a的是(外)参数标签(内部)变量名在同一时间。它是等效的 -func foo(a a: String)不会接受_.

Why is Apple using it?

苹果为什么要使用它?

You can see Apple is using it across the API. Apple's libraries are still written in Objective-C (if not, they share the same function names anyway, which were designed for Objective-C syntax)

您可以看到 Apple 在整个 API 中使用它。Apple 的库仍然是用 Objective-C 编写的(如果不是,它们仍然共享相同的函数名称,这些名称是为 Objective-C 语法设计的)

Functions like applicationWillResignActive(_ application: UIApplication)would have redundantparameter name application, since there is already the applicationin it's function name.

applicationWillResignActive(_ application: UIApplication)这样的函数会有多余的参数 name application,因为它的函数名称中已经有应用程序

Your example

你的榜样

func runAction(_ action: SKAction!)would be called withoutit's _mark like runAction(action:). The parameter name actionwould be redundantsince there is already one in the function name. That's the purpose and why it's there.

func runAction(_ action: SKAction!)将被称为没有它的_标志一样runAction(action:)。参数名称action将是多余的,因为函数名称中已经有一个。这就是目的以及它存在的原因。

回答by dasblinkenlight

An identifier in front of parameter declaration defines an externalparameter name. This is the name that must be provided by the caller when calling the function:

参数声明前面的标识符定义了一个外部参数名称。这是调用者在调用函数时必须提供的名称:

func someFunction(externalParameterName localParameterName: Int)

Swift provides an automatic external name for any defaulted parameter you define, if you do not provide an external name yourself. Using an underscore for the external parameter name opts out from this behavior:

如果您自己不提供外部名称,Swift 会为您定义的任何默认参数自动提供一个外部名称。使用下划线作为外部参数名称退出此行为:

You can opt out of this behavior by writing an underscore (_) instead of an explicit external name when you define the parameter.

您可以通过_在定义参数时编写下划线 ( ) 而不是显式外部名称来选择退出此行为。

You can read more about this behavior in the section on External Names for Parameters with Default Valueshere.

您可以在此处有关具有默认值的参数的外部名称部分中阅读有关此行为的更多信息。

回答by smileBot

I think this forces a convention in Swift that makes it read closer to objective-c, which matches cocoa conventions better. In objc you don't (externally) name your first parameter. Instead, by convention you usually include the external name in the latter part of the method name like this:

我认为这会强制 Swift 中的约定使其更接近于 Objective-c,从而更好地匹配可可约定。在 objc 中,您不会(从外部)命名您的第一个参数。相反,按照惯例,您通常在方法名称的后半部分包含外部名称,如下所示:

- (void)myFancyMethodWithFirstName:(NSString *)aFirstName lastName:(NSString *)aLastName;

[someInstance myFancyMethodWithFirstName:@"John" lastName:@"Doe"];

To make Swift api calls consistent with objc you will want to suppress the external parameter name of the first param.

为了使 Swift api 调用与 objc 一致,您需要取消第一个参数的外部参数名称。

func myFancyMethodWithFirstName(_ firstName:String, lastName:String);

someInstance.myFancyMethodWithFirstName("John", lastName:"Doe")

回答by fujianjin6471

Actually, there is a difference between the real code used to define a method and the method declaration in Apple's docs. Let's take UIControl's - addTarget:action:forControlEvents:method for example, the real code is: enter image description here

实际上,用于定义方法的实际代码与 Apple 文档中的方法声明之间存在差异。我们以UIControl- addTarget:action:forControlEvents:方法为例,真正的代码是: 在此处输入图片说明

But in docs, it appear like this (notice _ before target): enter image description here

但在文档中,它看起来像这样(注意 _ 在目标之前): 在此处输入图片说明

In real code, _ is used to make the second or subsequent parameter's external name not appear when a method is called, while in docs, _ before a parameter's local name indicates that when you call a method or a function, you should not provide an external name.

在实际代码中,_用于使调用方法时不出现第二个或后续参数的外部名称,而在文档中,_在参数的本地名称之前表示调用方法或函数时,不应提供外部名称。

There's no external name when a function is called by default unless you provide your own or add # before (without whitespace) a parameter's local name, for example, this is how we use dispatch_after: enter image description here

默认情况下调用函数时没有外部名称,除非您提供自己的名称或在参数的本地名称之前添加#(不带空格),例如,我们如何使用dispatch_after在此处输入图片说明

And in docs, it appear like this (notice three _): enter image description here

在文档中,它看起来像这样(注意三个 _): 在此处输入图片说明

The convention of function's declaration is just the same as I have described for method.

函数声明的约定与我在方法中描述的相同。

回答by Nik Kov

Just more visually.

只是更直观。

enter image description here

在此处输入图片说明

As you can see the _just make omit a local parameter name or not.

如您所见,_make 是否省略了本地参数名称。