Swift References 中的 _ 下划线代表什么? [英] What's the _ underscore representative of in Swift References?

查看:18
本文介绍了Swift References 中的 _ 下划线代表什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

func runAction(_action: SKAction!)

Objective-C 的等价物"是:

The Objective-C 'equivalent' of this is:

- (void)runAction:(SKAction *)action

让我觉得很重要的是(在 Swift 参考中)下划线后面有一个空格,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.

但我不明白这是想表达什么.所以也许问题是......是否有参考文献中使用的约定的参考?

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?

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

-- 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

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

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?

推荐答案

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

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

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

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

Swift 默认为 方法 中的第一个参数名称提供本地参数名称,并默认为第二个和后续参数名称本地和外部参数名称.

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

另一方面,函数默认没有外部参数名称.

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

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

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

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

当你调用 foo() 时,它的调用方式类似于 bar.foo("Hello", s2: "World").

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

但是,您可以通过在声明它的 s2 前面使用 _ 来覆盖此行为.

But, you can override this behavior by using _ in front of s2 where it's declared.

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

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

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

回到你的例子,runAction 是一种方法,因为它显然与 SKNode 类型相关联.因此,在参数 action 之前放置一个 _ 允许您在没有外部名称的情况下调用 runAction.

Back to your case, runAction is a method because it's associated with type SKNode, obviously. Thus, putting a _ before parameter action allows you to call runAction without an external name.

Swift 2.0 的更新

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

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

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

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

因此,通过在 函数 前面提供 _,调用者不必指定外部参数名称,就像您对 <强>方法.

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.

这篇关于Swift References 中的 _ 下划线代表什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆