4

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?

Pravin Tate
  • 1,145
  • 8
  • 18

6 Answers6

2

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.

avismara
  • 5,141
  • 2
  • 32
  • 56
2

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.

jboi
  • 11,324
  • 4
  • 36
  • 43
1

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 {
}
MobileGeek
  • 2,462
  • 1
  • 26
  • 46
1

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)
Morgan Wilde
  • 16,795
  • 10
  • 53
  • 99
0

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.

enter image description here

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

enter image description here

liushuaikobe
  • 2,152
  • 1
  • 23
  • 26
0

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.

Community
  • 1
  • 1
arthankamal
  • 6,341
  • 4
  • 36
  • 51
  • I am coding in playground not use any class – Pravin Tate Jul 28 '15 at 06:26
  • thought to write, @jboi answer. so i guest you got it. – arthankamal Jul 28 '15 at 06:30
  • Ya got it but I'm using xcode 6.4 but some one saying that in xcode 7.0 apple removes that # thing – Pravin Tate Jul 28 '15 at 06:32
  • i don't think they've removed it, if it so they must've changed the documents. check [Shorthand External Parameter Names](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html#//apple_ref/doc/uid/TP40014097-CH10-ID168) – arthankamal Jul 28 '15 at 06:37