Ryan Heitner
Ryan Heitner

Reputation: 13632

Method Naming Issues Swift

I wish 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 title title can be expressed more succinctly as #title

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

I get a warning extraneous '#' in parameter title is already the keyword argument name

Should I ignore these warnings and chalk it up to a bug in Beta ?

Upvotes: 6

Views: 1440

Answers (4)

Léo Natan
Léo Natan

Reputation: 57040

Name your method like so:

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

Then from Swift, call it like so:

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

And in Objective C:

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

Upvotes: 0

buley
buley

Reputation: 29208

Swift allows you to name arguments differently inside the function than out.

You'd use these "External Parameter Names" when when you wanted the argument to have a different name inside the function. From the book:

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.

Since you're using the same name both internally an externally here, "title," you can skip the external parameter name as Swift suggests and use the # prefix to denote a named external parameter with the same name referenced internally.

Example from the book:

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

Upvotes: 0

jacobhyphenated
jacobhyphenated

Reputation: 2145

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


From Apple's 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—amount and numberOfTimes. By default, Swift treats amount as a local name only, but treats numberOfTimes as both a local and an external name. You call the method as follows:

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

You don’t need to define an external parameter name for the first argument value, because its purpose is clear from the function name incrementBy. The second argument, however, is qualified by an external parameter name to make its purpose clear when the method is called.

This default behavior effectively treats the method as if you had written a hash symbol (#) before the numberOfTimes parameter


Basically, for Methods inside a class, the first parameter defaults to an internal parameter name only. All subsequent parameter names default to external names where the external name is the parameter name by default. Thus, the # is redundant.

func insertFileWithService(service: GTLServiceDrive, title: String)

Is equivalent to

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

For Methods, not for Functions. This is why you are getting a warning.

Upvotes: 5

Bill
Bill

Reputation: 45408

Leave the hash mark off and just use "title: String"

Upvotes: 0

Related Questions