Using Function Parameter Names in Swift

后端 未结 5 511
逝去的感伤
逝去的感伤 2020-12-10 13:10

In Swift parameter names are used when you call a method, except for the first parameter. Why is the first name not used?

Using a variation from the Swift manual;

相关标签:
5条回答
  • 2020-12-10 13:51

    Just to update this answer for the latest version of Swift, the use of # to force the usage of a function's first named parameter externally has been removed from the language. In order to specify external parameter names, you now simply separate the external name from the internal name with a space. The # was previously used to indicate a parameter with the same name both internally and externally, but now you have to double up the parameter name in the function definition in order to achieve this:

    var count2: Int = 0
    func incrementBy2(amount amount: Int, numberOfTimes times: Int) {
      count2 += amount * times
    }
    incrementBy2(amount: 2, numberOfTimes: 7) // result is 14
    

    You could also differentiate the names for the sake of clarity:

    var count2: Int = 0
    func incrementBy2(theValue amount: Int, numberOfTimes times: Int) {
      count2 += amount * times
      }
    incrementBy2(theValue: 2, numberOfTimes: 7)
    

    Or just do it the Objective-C way and make it clear what the first parameter should be:

    var count2: Int = 0
    func incrementBy2TheValue(amount: Int, numberOfTimes times: Int) {
      count2 += amount * times
    }
    incrementBy2TheValue(2, numberOfTimes: 7)
    

    I'm not sure how people feel about numerals in function names, however. But I realize this is just an example.

    0 讨论(0)
  • 2020-12-10 14:02

    It just uses the same pattern as in objective C, where methods have named parameters except for the first one, which is defined by the method name itself. For example

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    

    You can turn the first parameter into a named parameter by explicitly adding a name or by prefixing the parameter name with a hash # character.

    By the way, in objective C a method signature is the method name plus all named parameters, so for example:

    - (void) addUser:(User *)user toArray:(NSMutableArray *)array
    

    is different than:

    - (void) addUser:(User *)user toDict:(NSMutableDictionary *)dict
    

    pretty much as in other languages (C#, Java, etc.):

    void addUser(User user, Array array)
    

    is different than:

    void addUser(User user, Dictionary dict)
    
    0 讨论(0)
  • 2020-12-10 14:03

    This is to follow an convention we were all used to from Objective-C, where the name of the first parameter is combined with the method name. Here's an example:

    - (void)incrementByAmount:(NSInteger)amount
                numberOfTimes:(NSInteger)times
    {
        // stuff
    }
    

    You could call the method like:

    [self incrementByAmount:2 numberOfTimes:7];
    

    And it feels more natural to read by incorporating the name of the parameter into the method's name. In Swift, you can achieve the same with the following:

    func incrementByAmount(amount: Int, numberOfTimes: Int) {
        // same stuff in Swift
    }
    

    And call the method like:

    incrementByAmount(2, numberOfTimes: 7)
    

    If you don't want to use this convention, Swift gives you the ability to be more explicit and define separate internal and external parameter names, like so:

    func incrementByAmount(incrementBy amount: Int, numberOfTimes: Int) {
        // same stuff in Swift
        // access `amount` at this scope.
    }
    

    You can call the method like this:

    incrementByAmount(incrementBy: 2, numberOfTimes: 7)
    
    0 讨论(0)
  • 2020-12-10 14:05

    Methods in Swift are very similar to their counterparts in Objective-C. As in Objective-C, the name of a method in Swift typically refers to the method’s first parameter using a preposition such as with, for, or by, as seen in the incrementBy method from the preceding Counter class example. The use of a preposition enables the method to be read as a sentence when it is called. Swift makes this established method naming convention easy to write by using a different default approach for method parameters than it uses for function parameters.

    Specifically, Swift gives the first parameter name in a method a local parameter name by default, and gives the second and subsequent parameter names both local and external parameter names by default. This convention matches the typical naming and calling convention you will be familiar with from writing Objective-C methods, and makes for expressive method calls without the need to qualify your parameter names.

    Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/ru/jEUH0.l

    0 讨论(0)
  • 2020-12-10 14:10

    In your example you don't set the name for the first param. You have the variable name only.

    Try following please:

    func incrementBy2(amount am: Int, numberOfTimes times: Int) {
    }
    

    So you can call it as

    incrementBy2(amount: 0, numberOfTimes: 0)

    and am will be the name of the variable inside the function. amount will be an external name.

    If you want the same external name and variable, you can try this:

    func incrementBy2(amount amount: Int, numberOfTimes times: Int) {
    }
    

    And still call like

    incrementBy2(amount: 0, numberOfTimes: 0)

    More info Function Parameter Names and Specifying External Parameter Names chapters on Apple's The Swift Programming Language

    0 讨论(0)
提交回复
热议问题