Swift gives syntax error when calling func

I am new to fast and am tracking an apple document to study it. apple doc

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

I just copy the code from apple doc and try to run on the playground, but on the last line it gives me a syntax error and says it removes the day:. When I delete the day: in fun causing it to run flawlessly

 greet("Bob", "Tuesday") 

Is there any error in the apple document or am I doing something wrong?

+4
source share
6 answers

Naming function parameters works differently, depending on where the function is defined (and again different from initializers)

How do you determine that a function is global, outside the class . In this case, the function call does not determine the parameters.

If you define a function inside a class , your first attempt will work just fine. In the functions inside the class (methods), you specify the parameters, except for the first. If you want to also have a name, you should use

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

To complete all this, initializers need all the parameters. Even the first and even without #.

Does all this sound a bit confusing? Well, Apple had the same opinion, and according to what was said at WWDC 2015, they change the behavior in Swift 2 and make it more consistent.

+2
source

These are Quick "functions", not "methods." They do not need parameter names. You can add them to the declaration for readability, but you cannot use it during a call.

On the other hand, the "methods" of the class are called and work as you expected.

If you still need named parameters when calling, try the following:

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

This will give you the expected results.

Find this in the documentation:

However, these parameter names are used only in the body of the function itself and cannot be used when calling the function. These types of parameter names are called local parameter names, because they are only available for use in the body of functions.

A note about methods and functions: Simply put, methods belong to a class. There is no function. From the documentation:

Functions are self-contained code fragments that perform a specific task. You give the function a name that identifies what it does, and that name is used to β€œcall” the function to perform its task when necessary.

Note that Swift 2.0 has nothing to do between method calls and function calls, so in Swift 2.0 your path is valid.

+2
source

In function:

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

"name" and "day" are variables, not a label, as in Obj C. You can write "day" as a label:

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

You must understand that a new language such as Swift is bound to change. This is one of these changes that was introduced during WWDC 2015.

Apple has combined how you define and call functions and methods so that the behavior is the same. They completely removed the # definitions from func and demanded that the name of the first argument to func be included in the name, and all other arguments have either a name or not a name.

These are examples of valid func declarations in Swift 2.0 that should be released using 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 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) 
+1
source

This version of Swift made a difference.

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

Also, when I try to call greet as greet("Bob", "Tuesday") in Xcode 7.0 Beta, it gives me an error that is missing day:

enter image description here

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

enter image description here

0
source

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

You must have created a function , which means that you might have created it inside Viewdidload (this is my hunch). Therefore, you cannot specify a function parameter name.

if you created a method (outside Viewdidload ) you can call as you tried

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

or

Check Abbreviated external parameter names for external parameter names.

0
source

All Articles