Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method names in Swift

In Objective-C we have method names like application:didFinishLaunchingWithOptions:, but in Swift the method for the same job looks different.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    return true
}

Is the name of this method simply application since everything else are just parameters? Or is it called application didFinishLaunchingWithOptions with a space in the name? I was looking for an official answer in the Apple Documentation but I could not find one.

like image 999
TalkingCode Avatar asked Apr 05 '15 08:04

TalkingCode


People also ask

How do you name a method in Swift?

For method names, start with a lowercase letter, and capitalise the first letter of embedded words.

What are methods in Swift?

Methods are functions that are associated with a particular type. Classes, structures, and enumerations can all define instance methods, which encapsulate specific tasks and functionality for working with an instance of a given type.

What is Inout in Swift?

Swift inout parameter is a parameter that can be changed inside the function where it's passed into. To accept inout parameters, use the inout keyword in front of an argument. To pass a variable as an inout parameter, use the & operator in front of the parameter.

What is difference between method and function in Swift?

Functions are the properties of structured languages. Methods are the properties of Object-oriented language. It is a self-describing piece of code. It is used to manipulate the instance variable of a class.


2 Answers

The method is indeed called application, however didFinishLaunchingWithOptions is an external parameter name and:

If you provide an external parameter name for a parameter, that external name must always be used when you call the function.

Since there can be two functions called application with different external parameter names, we always have to specify the external parameters when referring to a function. So, the whole name of the function/method would be

application(_:didFinishLaunchingWithOptions:)

You are right there hasn't be any convention made yet for referring to Swift functions. The safest way to refer to a function now is to use the Obj-C convention.

application:didFinishLaunchingWithOptions:

Which is still used in all Apple documentation links.

This convention is used throughout Apple documentation.

like image 195
Sulthan Avatar answered Sep 18 '22 04:09

Sulthan


The method is indeed “just” application.
Swift uses this more often, if you have a tableview for example almost all functions start with tableview

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {}

The parameters “define” the functions instead of the method.

like image 41
milo526 Avatar answered Sep 19 '22 04:09

milo526