Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift function, two names for one parameter [duplicate]

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)
}
like image 860
jendan Avatar asked Aug 16 '15 20:08

jendan


People also ask

Can we use same named functions in more than one classes defined in a program?

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.

What is variadic function in Swift?

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.

Which of following techniques can be used to define 2 functions that have same name and are in same class?

Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters.

Can we pass function as a parameter in Swift?

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.


2 Answers

First name is public name, second - private (ca be used only in function)

like image 44
SwiftStudier Avatar answered Sep 28 '22 08:09

SwiftStudier


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

like image 80
0xJoKe Avatar answered Sep 28 '22 09:09

0xJoKe