I have noticed that some methods e.g. init(nibName nibName: String?, bundle nibBundle: NSBundle?)
has two "names" for one parameter except the first one is not possible to use inside method. In this case you are not able to use bundle
but can use nibBundle. For example, when I call super.init(nibName: nibName, bundle: bundle)
I get error "Use of unresolved identifier 'bundle'".
My question is: What is it (double named parameter) for? How to use it properly?
EDIT: It is now obvious it is External Parameter Names thing. I have subclass of UIViewController and override following method. I don't get where nibBundle gets from? Apparently it is not defined in function header.
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
let someBundle = nibBundle
print(someBundle)
}
Core Java bootcamp program with Hands on practice Yes, we can define multiple methods in a class with the same name but with different types of parameters.
In Swift, variadic parameters are the special type of parameters available in the function. It is used to accept zero or more values of the same type in the function. It is also used when the input value of the parameter is varied at the time when the function is called.
Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters.
Every function in Swift has a type, consisting of the function's parameter types and return type. You can use this type like any other type in Swift, which makes it easy to pass functions as parameters to other functions, and to return functions from functions.
First name is public name, second - private (ca be used only in function)
From Apple's documentation:
Sometimes it’s useful to name each parameter when you call a function, to indicate the purpose of each argument you pass to the function.
If you want users of your function to provide parameter names when they call your function, define an external parameter name for each parameter, in addition to the local parameter name. You write an external parameter name before the local parameter name it supports, separated by a space:
func someFunction(externalParameterName localParameterName: Int) { // function body goes here, and can use localParameterName // to refer to the argument value for that parameter }
Shorthand External Parameter Names
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.
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With