Swift Problem Naming Methods

I want to create a Swift method equivalent to

+ (void)insertFileWithService:(GTLServiceDrive *)service title:(NSString *)title 

When i type

 func insertFileWithService(service: GTLServiceDrive, title title: String, 

I get a warning header title, which can be expressed more succinctly: #title

But when I change it to func insertFileWithService (service: GTLServiceDrive, #title: String

I get an extraneous "#" warning in the parameter header already the name of the keyword argument

Should I ignore these warnings and flag them before the beta bug?

+6
source share
4 answers

I do not believe that this is a mistake, in fact, this is how the language was designed to work:


From the Apple Stuff ( https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Methods.html#//apple_ref/doc/uid/TP40014097-CH15-XID_300 ):

 class Counter { var count: Int = 0 func incrementBy(amount: Int, numberOfTimes: Int) { count += amount * numberOfTimes } } 

This incrementBy method has two parameters - number and number ofOfTimes. By default, Swift treats the number as a local name, but processes numberOfTimes both the local and the external name. You call the method as follows:

 let counter = Counter() counter.incrementBy(5, numberOfTimes: 3) // counter value is now 15 

You do not need to define the name of the external parameter for the value of the first argument, since its purpose is clear from the name of the function incrementBy. The second argument, however, qualifies by the name of an external parameter to clear its purpose when the method is called.

This default behavior effectively handles the method as if you had a hash character (#) before the numberOfTimes parameter


In principle, for methods inside a class, the first parameter by default has only the name of the internal parameter. All subsequent default parameter names correspond to external names, where the external name is used by default. So # is redundant.

 func insertFileWithService(service: GTLServiceDrive, title: String) 

Is equivalent

 func insertFileWithService(service: GTLServiceDrive, #title: String) 

For methods, not functions. That is why you get a warning.

+5
source

Leave a hash mark and just use "title: String"

0
source

Swift allows you to enter arguments differently inside a function than outside.

You should use these "External parameter names" when you want the argument to have a different name inside the function. From the book :

It is sometimes useful to specify each parameter when calling a function to indicate the purpose of each argument that you pass to the function.

If you want users of your function to provide parameter names when calling your function, define an external parameter name for each parameter, in addition to the local parameter name.

Since you use the same name, both inside and outside, β€œname”, you can skip the name of the external parameter, as Swift suggests, and use the prefix # to denote the named external parameter with the same name that is referenced internally.

Example from the book:

 func containsCharacter(#string: String, #characterToFind: Character) -> Bool { for character in string { if character == characterToFind { return true } } return false } 
0
source

Name your method as follows:

 @objc(insertFileWithService:title:) func insertFile(service: GTLServiceDrive, title: String) 

Then from Swift, name it like this:

 obj.insertFile(serviceDrive, title: "Title") 

And in Objective C:

 [obj insertFileWithService:serviceDrive title: @"Title"]; 
0
source

Source: https://habr.com/ru/post/970628/


All Articles