Quick Function Alias

How to create an alias for a function in swift?

for example

I want to call

LocalizedString("key") 

and he must call

 NSLocalizedString("key", comment:"") 

I saw the typealias command, but it looks like it only works for types.

+7
alias swift
source share
2 answers

Functions are called closures, so you can simply assign a function to a variable:

 let LocalizedString = NSLocalizedString 

You can create aliases for class and structure methods. Each method actually represents a static (cool) currency function, taking an instance of the class as the first parameter. Therefore, the class is given:

 class MyClass { var data: Int init(data: Int) { self.data = data } func test() { println("\(data)") } } 

you can assign the test method to a variable:

 let test = MyClass.test 

and then call it like:

 var instance = MyClass(data: 10) test(instance)() 

UPDATE

I just realized that I missed one important detail in your question: you want to hide the comment parameter. And my proposed solution does not allow this, while the @rintaro solution does.

However, for this I use a different approach: I create a String extension that implements a computed property:

 extension String { var localized: String { return NSLocalizedString(self, comment: "") } } 

and then I can just call it for any string variable or literal:

 var string = "test_resource" string.localized "another_resource".localized 
+15
source share

The shortest of them:

 let LocalizedString = { NSLocalizedString($0, comment:"") } 

But this is really a new feature. Just wrap the NSLocalizedString .


Perhaps you can use the undocumented @transparent attribute. It embeds a function call. see this topic in the developer forum .

 @transparent LocalizedString(key:String) -> String { return LocalizedString(key, comment:"") } 

But this is not recommended. Moreover, as long as my tests, all subsequent codes eventually emit the exact LLVM IR code with -O optimization.

script1 : with @transparent

 import Foundation @transparent func LocalizedString(key:String) -> String { return LocalizedString(key, comment:"") } println(LocalizedString("key")) 

script2 : without @transparent

 import Foundation func LocalizedString(key:String) -> String { return LocalizedString(key, comment:"") } println(LocalizedString("key")) 

script3 : direct call to NSLocalizedString

 import Foundation func LocalizedString(key:String) -> String { return LocalizedString(key, comment:"") } println(NSLocalizedString("key", comment:"")) 

All of the above is intended to make a direct call to NSLocalizedString .

But the following code is different from the other:

script4 : closing the package

 import Foundation let LocalizedString = { NSLocalizedString($0, comment:"") } println(NSLocalizedString("key", comment:"")) 

It is also built-in, but an additional refcount statement has been added to LocalizedString .

So, as a conclusion, you should just use this:

 func LocalizedString(key:String) -> String { return LocalizedString(key, comment:"") } 
+6
source share

All Articles