How to Use Keywords as Parameter Names in Swift

Can we use keywords as parameter names in SWIFT?

When using a keyword as a normal identifier you have to escape it using backticks ` like this

func setupTable(for: Genre) {
switch `for` {
case .drama: break
case .comedy: break
}
}

On a side note, as pointed out by @Hamish in a comment on the question, you should try to avoid using such names for variables, which (in this case) you can do by providing both an internal and external name for the parameter:

func setupTable(for genre: Genre) {
switch genre {
case .drama: break
case .comedy: break
}
}

How to declare keyword variable in Swift

var `extension`: String?
var username: String?

init(`extension`: String, username: String) {
self.`extension` = `extension`
self.username = username
}

I think this will work by using ``.

Swift with keyword

with is not a keyword - it's just an external parameter identifier. This works as well:

func toDebugString<T>(whatever x: T) -> String

Since the toDebugString<T>(x: T) function is already defined, by using an external parameter you are creating an overload: same function name, but different parameters. In this case the parameter is the same, but identified with an external name, and in swift that makes it a method with a different signature, hence an overload.

To prove that, paste this in a playground:

func toDebugString<T>(# x: T) -> String {
return "overload"
}

toDebugString(x: "t") // Prints "overload"
toDebugString("t") // Prints "t"

The first calls your overloaded implementation, whereas the second uses the existing function

Suggested reading: Function Parameter Names

swift argument labels and keywords

or just that these keywords can also be used as argument labels?

Exactly. This is introduced in SE-0001 Allow (most) keywords as argument labels. The motivation is that:

Sometimes, the most natural label for an argument coincides with a language keyword, such as in, repeat, or defer. Such keywords should be allowed as argument labels, allowing better expression of these interfaces.

Though in SE-0001 it is said that inout, var and let are not allowed to be argument labels (back then they are used to describe the mutability of the parameter). This restriction was later relaxed. Now, only inout is not allowed as an argument label, and you get a warning if you use var or let as an argument label.

What are the new for , at , in keywords in Swift3 function declarations?

The syntax allow you to label arguments twice - once for use within the function (the parameter name) and once for use when calling (the argument label). By default these two will be the same (like with sender in your example), but they differ when it makes a function more readable at the call site:

prepare(for: aSegue, sender: something)

being more readable-as-a-sentence than:

prepare(segue: aSegue, sender: something)

Which sounds like you're preparing the segue, not preparing for the segue.

for would be a dreadful name to use internally in the function to refer to the segue, so you can use a different one if you require.

The idea is to meet the sometimes conflicting goals of readability at the call site and sensible naming in the implementation.

When defining a function, if you are going to use separate argument labels and parameter names, you specify the argument label first, and the parameter name second:

func sayHello(to name: String) {
print("Hello " + name)
}

sayHello(to: "Pekka")

to only has relevance when calling the function. name is used inside the function.

Use reserved keyword a enum case

From the Swift Language Guide (Naming Constants & Variables section)

If you need to give a constant or variable the same name as a reserved Swift keyword, surround the keyword with back ticks (`) when using it as a name. However, avoid using keywords as names unless you have absolutely no choice.

enum MyEnum {
case `Self`
case AnotherCase
}

and use it with or without backticks

let x: MyEnum = .Self
let y = MyEnum.`Self`

Can I use reserved keywords in NS_SWIFT_NAME?

The problem is not the keyword, but that you specified both argument
label and parameter name in NS_SWIFT_NAME. The argument label
is sufficient. With

@protocol MyContainerDelegate
-(NSInteger)myContainerContentsCount:(MyContainer *)container
NS_SWIFT_NAME(contentsCount(in:));
@end

the protocol is exported to Swift as

public protocol MyContainerDelegate {
public func contentsCount(in container: MyContainer!) -> Int
}

What is the keyword repeating used for in Swift?

It's not a keyword, it's an optional function argument label that can differ from the local parameter name that's used inside the function/method.

Read the section Function Argument Labels and Parameter Names in The Swift Programming Language:

Each function parameter has both an argument label and a parameter name. The argument label is used when calling the function; each argument is written in the function call with its argument label before it. The parameter name is used in the implementation of the function. By default, parameters use their parameter name as their argument label. …

You write an argument label before the parameter name, separated by a space …

If you don’t want an argument label for a parameter, write an underscore (_) instead of an explicit argument label for that parameter.

Swift `in` keyword meaning?

In a named function, we declare the parameters and return type in the func declaration line.

func say(s:String)->() {
// body
}

In an anonymous function, there is no func declaration line - it's anonymous! So we do it with an in line at the start of the body instead.

{
(s:String)->() in
// body
}

(That is the full form of an anonymous function. But then Swift has a series of rules allowing the return type, the parameter types, and even the parameter names and the whole in line to be omitted under certain circumstances.)

Swift variable name with ` (backtick)

According to the Swift documentation :

To use a reserved word as an identifier, put a backtick (`)before and after it. For example, class is not a valid identifier, but `class` is valid. The backticks are not considered part of the identifier; `x` and x have the same meaning.

In your example, default is a Swift reserved keyword, that's why backticks are needed.



Related Topics



Leave a reply



Submit