简体   繁体   English

在func调用时,Swift会出现语法错误

[英]Swift gives syntax error while func calling

I am new in swift and I am following apples doc for studying it. 我是快速的新人,我正在跟随苹果医生研究它。 apple doc 苹果医生

func greet(name: String, day: String) -> String {
    return "Hello \(name), today is \(day)."
}
greet("Bob", day: "Tuesday")

I just copy above code from apple doc and try to run in playground but in last line its gives me syntax error and telling that remove day: in that. 我只是从apple doc复制上面的代码并尝试在操场上运行,但在最后一行它给我语法错误并告诉删除日:在那。 When I am remove day: in fun calling its run perfectly 当我被删除的一天:在乐趣称其运行完美

greet("Bob", "Tuesday")

Is there any mistake in apple doc or I am doing something wrong? 苹果医生是否有任何错误,或者我做错了什么?

These are Swift "functions" not "methods". 这些是Swift“函数”而不是“方法”。 They do not need parameter names. 它们不需要参数名称。 You can still add them in the declaration for readability, but you cannot use it while calling. 您仍然可以在声明中添加它们以便于阅读,但在调用时无法使用它。

On the other hand, class "methods" are named and work as you expected. 另一方面,类“方法”被命名并按预期工作。

If you still need named parameters while calling, try this: 如果在调用时仍需要命名参数,请尝试以下操作:

func greet(name: String, #day: String) -> String {
    return "Hello \(name), today is \(day)."
}
greet("Bob", day: "Tuesday")

This will give you expected results. 这将为您提供预期的结果。

Find this in documentation: 在文档中找到:

However, these parameter names are only used within the body of the function itself, and cannot be used when calling the function. 但是,这些参数名称仅在函数本体中使用,并且在调用函数时不能使用。 These kinds of parameter names are known as local parameter names, because they are only available for use within the function's body. 这些参数名称称为本地参数名称,因为它们仅可在函数体内使用。

A note on methods and functions: Simply put, methods belong to a class. 关于方法和函数的注释:简单地说,方法属于一个类。 Functions don't. 功能没有。 From documentation: 来自文档:

Functions are self-contained chunks of code that perform a specific task. 函数是执行特定任务的自包含代码块。 You give a function a name that identifies what it does, and this name is used to “call” the function to perform its task when needed. 您为函数指定了一个标识其功能的名称,此名称用于“调用”函数以在需要时执行其任务。

Note that Swift 2.0 makes no difference between method calls and function calls, so in Swift 2.0, your way is valid. 请注意,Swift 2.0在方法调用和函数调用之间没有区别,因此在Swift 2.0中,您的方式是有效的。

Naming the parameters of a function works different, depending on where the function is defined (and again different on initializers) 命名函数的参数工作方式不同,具体取决于函数的定义位置(在初始化程序中也是如此)

The way, you defined the function is global, outside of a class . 顺便说一下,你定义的函数是全局的,在类之外 In this case the function call does not name parameters. 在这种情况下,函数调用不会命名参数。

If you would define the function inside a class , your first try would work perfectly fine. 如果你要在一个类中定义函数 ,你的第一次尝试将完美地工作。 In functions inside a class (methods) you name the parameters except the first one. 在类(方法)中的函数中,您可以命名除第一个参数之外的参数。 If you would like to have the first name, too, you would use 如果你想拥有名字,你也可以使用

func greet(#firstParameter: String, secondParameter: String) ...

And to complete all that, initializers need all parameters named. 为了完成所有这些, 初始化程序需要所有名为的参数。 Even the first one and even without the #. 即使是第一个甚至没有#。

Does all that sound a bit confusing? 这一切听起来有点令人困惑吗? Well, Apple had the same opinion and according to what was said on WWDC 2015 they change the behavior in Swift 2 and make it more consistent. 好吧,Apple有同样的看法,根据WWDC 2015上的说法,他们改变了Swift 2中的行为并使其更加一致。

In function: 功能:

func greet(name: String, day: String) -> String {
}

"name" and "day" are variables not the label as in Obj C. You can write "day" as label as: “name”和“day”是变量而不是Obj C中的标签。您可以将“day”写为标签:

func greet(name: String, day day: String) -> String {
}

You have to understand that a new language like Swift is bound to change. 你必须明白像Swift这样的新语言必然会改变。 This is one of those changes, it was introduced during WWDC 2015. 这是其中一项变更,它是在WWDC 2015期间推出的。

Apple has unified how you define and call functions and methods, so that the behaviour is the same. Apple统一了您定义和调用函数和方法的方式,因此行为是相同的。 They completely removed the # from func definitions and made it a requirement that the name of the 1st argument of func be included in the name, and the all other arguments have the option of either having a name, or not. 他们完全删除#func定义,并使其认为的第一个参数的名称要求func列入名,而所有其他参数要么有一个名字,或者没有的选项。

These are examples of valid func declarations in Swift 2.0 , which is to be released with Xcode 7 and iOS 9. 这些是Swift 2.0中有效func声明的示例,它将与Xcode 7和iOS 9一起发布。

func moveShapeToPosition(position: CGPoint, andScaleBy scale: CGFloat)
func moveShapeToPosition(position: CGPoint, scale: CGFloat)
func moveShapeToPosition(position: CGPoint, _ scale: CGFloat)

And to call them. 并打电话给他们。

moveShapeToPosition(CGPoint(x: 0, y: 0), andScaleBy: 1)
moveShapeToPosition(CGPoint(x: 0, y: 0), scale: 1)
moveShapeToPosition(CGPoint(x: 0, y: 0), 1)

It's the version of Swift made the difference. 这是Swift的版本与众不同。

I run the demo in Xcode 7.0 Beta, it works perfectly. 我在Xcode 7.0 Beta中运行该演示,它运行得很好。

Besides, when I try to call greet like greet("Bob", "Tuesday") in Xcode 7.0 Beta, it gives me the error that the day: is missing. 此外,当我尝试在Xcode 7.0 Beta中greet greet("Bob", "Tuesday")时,它给了我错误的那day:缺失。

在此输入图像描述

However, when I run it in the Xcode 6.4, it complaints the same as yours. 但是,当我在Xcode 6.4中运行它时,它和你的一样抱怨。

在此输入图像描述

In Swift, functions and methods are different. 在Swift中,函数和方法是不同的。 you can find detailed answer here . 你可以在这里找到详细的答案

You must be created function which means you may have created it inside Viewdidload (it's my guess). 你必须创建function ,这意味着你可能已经在Viewdidload创建了它(这是我的猜测)。 So you can't specifically give the function parameter name. 所以你不能专门给出函数参数名。

if you've created method (outside the viewdidload ) then you can call like you've tried 如果你已经创建了method (在viewdidload之外),那么你可以像你尝试过一样打电话

greet("Bob", day: "Tuesday")

or 要么

Check for Shorthand External Parameter Names for external parameter names. 检查外部参数名称的速记外部参数名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Swift中调用已定义的func时出现奇怪的xcode错误 - Odd xcode error when calling defined func in Swift 从目标c调用swift函数会导致链接器错误 - Calling swift function from objective c gives linker error 从另一个班级的另一个班级调用func-Swift - Calling a func from one class in another class - swift Swift语法错误和init()? - Swift Syntax Error and init()? 有没有办法在使用 Swift 的 func viewDidLoad() 上导航到另一个页面? - Is there a way to navigate to another page while on func viewDidLoad() using Swift? NSUserDefaults给Swift中的NSArray错误 - NSUserDefaults gives error with NSArray in Swift Alamofire在Swift 3,Xcode 8中出错 - Alamofire gives error in Swift 3, Xcode 8 覆盖func invalidateIntrinsicContentSize()给出不覆盖错误。 删除覆盖会与超类错误冲突 - override func invalidateIntrinsicContentSize() gives does not override error. Removing override gives conflicting with superclass error Swift 中的非英文字符给出:致命错误:在展开可选值时意外发现 nil - Non english character in Swift gives: Fatal error: Unexpectedly found nil while unwrapping an Optional value 调用另一个视图控制器的func来控制alpha会返回nil错误 - Calling func of another view controller to control alpha returns nil error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM