Thursday, 15 March 2012

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



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

i have seen code this:

class="lang-js prettyprint-override">func hello(name: string, #hellomessage: string) -> string { homecoming "\(hellomessage), \(name)." }

my question # mark means before parameter's name? meaning parameter has specified when calling function?

moreover can show me difference function without # mark? code examples more welcome.

if want provide external parameter name function parameter, , local parameter name appropriate name use, not need write same name twice parameter. instead, write name once, , prefix name hash symbol (#). tells swift utilize name both local parameter name , 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

update

for functions: when function called , purpose of parameters unclear, provide external names parameters.

func somefunction(parametername: int) { parametername } somefunction(5) // meaning of "5"? func somefunction(externalparametername parametername: int) { parametername } somefunction(externalparametername: 5) // it's clear.

but if external , local names same, write hash symbol before parameter name.

func somefunction(#parametername: int) { parametername } // it's like: // func somefunction(parametername parametername: int) { parametername } somefunction(parametername: 5)

for methods: default first parameter name local (like functions), sec , subsequent parameter names both local , external (like write hash symbol before parameter name, # implicitly there):

class someclass { func somemethodwith(firstparameter: int, andsecondparameter: int) { ... } } someclass().somemethodwith(5, andsecondparameter: 10)

you can utilize # (or add together explicit external name) first parameter of method too, it'll not match objective-c-style calling.

class someclass { func somemethodwith(#firstparameter: int, andsecondparameter: int) { ... } } someclass().somemethodwith(firstparameter: 5, andsecondparameter: 10)

swift

No comments:

Post a Comment