What Do "_" and "In" Mean in Swift Programming Language

what do _ and in mean in Swift programming language?

_ means don't name that thing. It can be used in a number of places. In your case, it is saying ignore the variable being passed into the closure. The code you gave is ignoring all parameters but you can also just ignore some parameters.

in is the start of the implementation of the closure. In your example code, the implementation of the closure is empty.

Overall, that line is defining a closure called "done" that takes an Optional NSError (NSError?), NSData (NSData), and Optional NSString (NSString?) and returns nothing (-> ()). The actual implementation of the closure does nothing.

What does an ampersand (&) mean in the Swift language?

It works as an inout to make the variable an in-out parameter. In-out means in fact passing value by reference, not by value. And it requires not only to accept value by reference, by also to pass it by reference, so pass it with & - foo(&myVar) instead of just foo(myVar)

As you see you can use that in error handing in Swift where you have to create an error reference and pass it to the function using & the function will populate the error value if an error occur or pass the variable back as it was before

Why do we use it? Sometimes a function already returns other values and just returning another one (like an error) would be confusing, so we pass it as an inout. Other times we want the values to be populated by the function so we don't have to iterate over lots of return values, since the function already did it for us - among other possible uses.

I hope that helps you!

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

& in swap(&someInt, &anotherInt) . What does it represent? What is its function?

Just like passing by reference in C++, the ampersands in the calling bit of the code just tell the compiler that you give permission to function swapTwoInts to change both someInt and anotherInt. If you had not put the ampersands there, the code would not compile.

Swift Programming language, what does it mean to start an attribute with a period

Note: TLDR version at bottom

This is called Implicit member expression.

Implicit Member Expression

An implicit member expression is an abbreviated way to access a member of a type, such as an enumeration case or a type method, in a context where type inference can determine the implied type.

So as long as Swift knows the type you can often omit typing it, such as

let colour: UIColor = .red  

It is often used for enum values and static types and can be used to pass in parameters to functions like your DispatchQueue example above. Another simplified example would be:

enum Colors {
case yellow, blue, red, green
}

So if you had this function..

func draw(withColor: Colors) {

}

you should be able to pass in the enum with the .prefix, like so

draw(withColor: .yellow)

The more verbose way in which this is a shortcut to would be:

draw(withColor: Colors.yellow)

Solution

To answer your question specifically, the .userInitiated here is actually a static property on DispatchQoS so its just a shorter way of typing DispatchQoS.userInitiated. Swift lets you shorten it becase it knows the type that is expected is of type DispatchQoS

What does _: mean in Swift?

It's a way of stating the name of the function. The function might be declared like this:

func recordResponse(s:String) {
// ...
}

Now, by default, the s is an internal parameter name only; the external parameter name is suppressed. So we need a notation that describes the function, recordResponse, as taking one parameter with no external name. That notation is recordResponse(_:). The colon follows each parameter name — here there is only one parameter, and has no external name, which is what the underscore indicates.

In the usage you are asking about, this notation is just that: a notation. It's a convention for giving the name of the function in a complete way, when humans talk to humans (as in a tutorial). But in Swift 2.2, this notation will become very important, because it will be part of the language — it's how you'll form a function reference. That is, it will be Swift's own name for the function.

What does \. mean in Swift

See Key-Path Expression in the Expressions section of the Language Reference in The Swift Programming Language.

What is the meaning of the '#' mark in swift language

Update (Swift 3.*...)

the default behavior of the first parameter’s signature was changed drastically. To understand how argument labels (ex. “external parameters”) and parameter names (ex. “local parameters”) work, please read the chapter “Function Argument Labels and Parameter Names” from the Apple’s Swift-book.

Some examples:

func someFunction(parameterName: Int) { parameterName }
someFunction(parameterName: 5) // argument label not specified

func someFunction(argumentLabel parameterName: Int) { parameterName }
someFunction(argumentLabel: 5) // argument label specified

func someFunction(_ parameterName: Int) { parameterName }
someFunction(5) // argument label omitted

There is no difference in this behavior between methods and functions.


Update (Swift 2.*)

The feature described below was deprecated, one need to write the parameter name twice to get the same behavior as with hash symbol before.


Update (examples)

For functions: when the function is called and purpose of some parameters is unclear, you provide external names for those parameters.

func someFunction(parameterName: Int) { parameterName }
someFunction(5) // What is the meaning of "5"?

func someFunction(externalParameterName parameterName: Int) { parameterName }
someFunction(externalParameterName: 5) // Now it's clear.

But if external and local names are the same, you just write a hash symbol before the parameter name.

func someFunction(#parameterName: Int) { parameterName }
// It's actually like:
// func someFunction(parameterName parameterName: Int) { parameterName }
someFunction(parameterName: 5)

For methods: by default first parameter name is only local (like by functions), but second and subsequent parameter names are both local and external (like as you write a hash symbol before the parameter name, this # is implicitly there):

class SomeClass {
func someMethodWith(firstParameter: Int, andSecondParameter: Int) { ... }
}
SomeClass().someMethodWith(5, andSecondParameter: 10)

You can use # (or add an explicit external name) for the first parameter of the method too, but it'll not match Objective-C-style calling.

class SomeClass {
func someMethodWith(#firstParameter: Int, andSecondParameter: Int) { ... }
}
SomeClass().someMethodWith(firstParameter: 5, andSecondParameter: 10)

Original answer

If you want to provide an external parameter name for a function
parameter, and the local parameter name is already an appropriate name
to use, you do not need to write the same name twice for that
parameter. Instead, write the name once, and prefix the name with a
hash symbol (#). This tells Swift to use that name as both the local
parameter name and the external parameter name.

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itunes.apple.com/ru/book/swift-programming-language/id881256329?l=en&mt=11

What function does as in the Swift syntax have?

The as keyword is used for casting an object as another type of object. For this to work, the class must be convertible to that type.

For example, this works:

let myInt: Int = 0.5 as Int // Double is convertible to Int

This, however, doesn't:

let myStr String = 0.5 as String // Double is not convertible to String

You can also perform optional casting (commonly used in if-let statements) with the ? operator:

if let myStr: String = myDict.valueForKey("theString") as? String {
// Successful cast
} else {
// Unsuccessful cast
}

In your case, touches is (I'm assuming from the anyObject() call) an NSSet. Because NSSet.anyObject() returns an AnyObject?, you have to cast the result as a UITouch to be able to use it.

In that example, if anyObject() returns nil, the app will crash, because you are forcing a cast to UITouch! (explicitly unwrapping). A safer way would be something like this:

if let touch: UITouch = touches.anyObject() as? UITouch {
// Continue
}


Related Topics



Leave a reply



Submit