简体   繁体   中英

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

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.

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:

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

You have to understand that a new language like Swift is bound to change. This is one of those changes, it was introduced during WWDC 2015.

Apple has unified how you define and call functions and methods, so that the behaviour is the same. 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.

These are examples of valid func declarations in Swift 2.0 , which is to be released with Xcode 7 and 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.

I run the demo in Xcode 7.0 Beta, it works perfectly.

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.

在此输入图像描述

However, when I run it in the Xcode 6.4, it complaints the same as yours.

在此输入图像描述

In Swift, functions and methods are different. you can find detailed answer here .

You must be created function which means you may have created it inside Viewdidload (it's my guess). 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

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

or

Check for Shorthand External Parameter Names for external parameter names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM