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

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

问题描述

在苹果文档的参考章节中,有很多这类事情的实例:


func runAction (_ action :SKAction!)


这个Objective-C的等价物是:

lockquote

- (void)runAction:(SKAction *) action


在我看来,重要的是(在Swift引用中)下划线后面有一个空格,action是用斜体字写的。 / p>

但我无法弄清楚这是在传达什么。所以也许问题是......是否有引用中使用的约定的引用?



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


更新



Swift 3对如何使用和命名函数/方法参数名称和参数标签进行了一些更改。这对这个问题及其答案有影响。 @Rickster在回答关于_underscore的另一个问题方面做得非常出色,可以清除大部分这些问题,这里:为什么我需要swift下划线?

解决方案

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

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



方法的本地和外部参数名称部分的文档,它表示:


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


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



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

  class Bar {
func foo(s1:String,s2:String) - >字符串{
返回s1 + s2;


$ / code>

当您调用 foo( ),它被称为 bar.foo(Hello,s2:World)



,您可以通过在 s2 _ 来覆盖此行为

  func foo(s1:String,_ s2:String) - >字符串{
返回s1 + s2;

然后,当您调用 foo ,它可以简单地称为 bar.foo(Hello,World),而不需要第二个参数的名称。 b

回到您的案例, runAction 是一种方法,因为它与 SKNode 类型相关联,明显。因此,在参数 action 之前放置 _ ,您可以调用 runAction 没有外部名称。

Swift 2.0更新



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



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



因此,通过在标签前提供 _ 函数,调用者不必指定外部参数名称,就像您为方法所做的操作一样。


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

func runAction(_action: SKAction!)

The Objective-C 'equivalent' of this is:

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

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

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?

解决方案

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 Methods section of the documentation, it says:

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.

For example, we have this foo() method defined in 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").

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

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

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.

Update for Swift 2.0

Function and method now work the same way in 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.

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引用中的下划线代表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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