What is the meaning of the '#' mark in swift language

前端 未结 5 955
天涯浪人
天涯浪人 2020-11-28 13:37

I have seen code like this:

func hello(name: String, #helloMessage: String) -> String { 
    return \"\\(helloMessa         


        
相关标签:
5条回答
  • 2020-11-28 13:47

    **

    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.

    This example defines a function called containsCharacter, which defines external parameter names for both of its parameters by placing a hash symbol before their local parameter names:

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

    This function’s choice of parameter names makes for a clear, readable function body, while also enabling the function to be called without ambiguity:

    let containsAVee = containsCharacter(string: "aardvark", characterToFind: "v")
    // containsAVee equals true, because "aardvark" contains a "v”
    

    Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks.

    https://itun.es/in/jEUH0.l

    0 讨论(0)
  • 2020-11-28 13:50

    With previous versions of Swift everything what @ovejka posted was true. In Swift 2.0 and above it looks like # is unused (in this or similar context, not counting #available)

    0 讨论(0)
  • 2020-11-28 13:51

    This has changed in Swift 2:

    now you specify an external parameter name before the internal one, instead of using # to force the existing name.


    According to the convention, the method name contains the action verb and the first parameter is not specified when calling the method:

    func sayHiFrom(sender: String, to: String) {
        print("Hi from \(sender) to \(to)!")
    }
    
    sayHiFrom("Jules", to: "Jim")
    

    Specifying an internal parameter name

    This time the second parameter has a different name for using inside the method, without changing the external name. When there is two names for a parameter, the first one is external and the second one is internal:

    func sayHiFrom(sender: String, to receiver: String) {
        print("Hi from \(sender) to \(receiver)!")
    }
    
    sayHiFrom("Jane", to: "John")
    

    Forcing an external parameter name

    You can force the first parameter to have an external name:

    func sayHi(from sender: String, to receiver: String) {
        print("Hi from \(sender) to \(receiver)!")
    }
    
    sayHi(from: "Joe", to: "Jack")
    

    In this case, it's better that the method name does not contain the action term, because the forced parameter name already plays its role.

    Forcing no external parameter names

    You can also remove the parameter name for other parameters by preceding them with _ (underscore):

    func sayHi(sender: String, _ receiver: String) {
        print("Hi from \(sender) to \(receiver)!")
    }
    
    sayHi("Janice", "James")
    
    0 讨论(0)
  • 2020-11-28 14:02

    I agree. It is confusing, but I hope this helps clear it up.

    If you don't use #, you can enter parameters without using their names, as long as they are in order:

    func x(a:Int, b:Int, c:Int) {some code}
    x(10,20,30) or x(a:10, b:20, c:30) //Still have to be in order!
    

    ...but if you add #, the first variant is not allowed, i.e. x(10,20,30), as Swift now forces labels to be used. You can only use x(a:10, b:20, c:30).

    Why would that be? Clarity. Makes code more readable.

    0 讨论(0)
  • 2020-11-28 14:04

    Update (Swift 3.*...)

    the default behavior of the first parameter’s signature was changed drastically. To understand how argument labels (ex. “external parameters”) and parameter names (ex. “local parameters”) work, please read the chapter “Function Argument Labels and Parameter Names” from the Apple’s Swift-book.

    Some examples:

    func someFunction(parameterName: Int) { parameterName }
    someFunction(parameterName: 5) // argument label not specified
    
    func someFunction(argumentLabel parameterName: Int) { parameterName }
    someFunction(argumentLabel: 5) // argument label specified
    
    func someFunction(_ parameterName: Int) { parameterName }
    someFunction(5) // argument label omitted
    

    There is no difference in this behavior between methods and functions.


    Update (Swift 2.*)

    The feature described below was deprecated, one need to write the parameter name twice to get the same behavior as with hash symbol before.


    Update (examples)

    For functions: when the function is called and purpose of some parameters is unclear, you provide external names for those parameters.

    func someFunction(parameterName: Int) { parameterName }
    someFunction(5) // What is the meaning of "5"? 
    
    func someFunction(externalParameterName parameterName: Int) { parameterName }
    someFunction(externalParameterName: 5) // Now it's clear.
    

    But if external and local names are the same, you just write a hash symbol before the parameter name.

    func someFunction(#parameterName: Int) { parameterName }
    // It's actually like:
    // func someFunction(parameterName parameterName: Int) { parameterName }
    someFunction(parameterName: 5)
    

    For methods: by default first parameter name is only local (like by functions), but second and subsequent parameter names are both local and external (like as you write a hash symbol before the parameter name, this # is implicitly there):

    class SomeClass {
        func someMethodWith(firstParameter: Int, andSecondParameter: Int) { ... }
    }
    SomeClass().someMethodWith(5, andSecondParameter: 10)
    

    You can use # (or add an explicit external name) for the first parameter of the method too, but it'll not match Objective-C-style calling.

    class SomeClass {
        func someMethodWith(#firstParameter: Int, andSecondParameter: Int) { ... }
    }
    SomeClass().someMethodWith(firstParameter: 5, andSecondParameter: 10)
    

    Original answer

    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.

    Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itunes.apple.com/ru/book/swift-programming-language/id881256329?l=en&mt=11

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