How to access custom function from any file in the same Swift project?

If the postfix operator of a user-defined function is declared in a file area - as in my previous post - there should be a way to access such a function from another Swift 3 in the same Xcode project.

The documentation for apples on user-defined functions states that "New operators are declared globally ..." , so I can imagine the creators of Swift 3 will be considered important for accessing user-defined functions around the world. But for beginners in Swift, it’s not obvious how I do it.

Based on the code in my previous accepted answer ( here ), what do I need to do so that the user-defined function can be accessed from another file elsewhere in the project?


FINAL IMAGE

The solution is awkwardly simple (see the accepted answer ), and hopefully other Swift newbies can also be useful.

A postfix operator % declared globally, and Extension of Int reads the Int variable from another file. Extensions to Double and CGFloat , as well as Int .

CustomOperators.swift

 import UIKit postfix operator % extension Double { static postfix func % (n: Double) -> Double { return Double(n) / 100 } } extension Int { static postfix func % (n: Int) -> Double { return Double(n) / 100 } } extension CGFloat { static postfix func % (n: CGFloat) -> Double { return Double(n) / 100 } } 

ViewController.swift

 import UIKit class ViewController: UIViewController { let test1: Double = 93.7 let test2: Int = 80 let test3: CGFloat = 70 override func viewDidLoad() { super.viewDidLoad() print(test1%) // prints '0.937' print(test2%) // prints '0.8' print(test3%) // prints '0.7' } } 

Thanks to marosoiae, as well as Martin R and a million for your input.


EDIT 2

Martin Rs comments prompted me to try another example with my custom postfix function defined in Utils , but in the file area, not as a static postfix function. Utils becomes an empty class for user-defined functions defined in the content area outside the class.

 import UIKit class ViewController: UIViewController { let value: Int = 25 override func viewDidLoad() { super.viewDidLoad() example1() example2() } func example1() { // 1. call to a function in Utils with an explicit value // and nothing returned from Utils. // Note this uses literal postfix syntax ie 25% Utils.25% // Debug message: // Type 'Utils' has no member '25' } func example2() { // 2. call to a function in Utils with an argument // declared in UIViewController and nothing returned from Utils Utils.(Int: value)% // Debug message // Expected member name following '.' } } 

Utils.swift now compiles, but from the debug messages that Xcode reports with both examples, it is clear that the token β€œ % ” is expected immediately after the β€œ . ”.

Without even returning a variable from Utils.swift , this is obviously not how to call the postfix function in Utils.swift from a file elsewhere in the project. My question remains: how do I do this?


EDIT 1

At this point, it may help to include some code, so I created a class with static functions, following the syntax (as far as possible) used in the Unniversal answer. I decided to try static functions if they help to avoid cluttering the global scope with methods that should belong to the class / structure.

File 1 - Utilities

 import UIKit postfix operator % class Utils { static func yourFunction(){ //Do your stuff here } static func yourFunction1(value: Int) { print(value) } static postfix func % (percentage: Int) -> Double { return (Double(percentage) / 100) } } 

Static function % reports

  Member operator '%' must have at least one argument of type 'Utils' 

which Id expects to disappear after the argument is passed from another file.

The following code shows four ways in which I tried to provide an argument to static functions in Utils. I ran the project with one example at a time

File 2 - UIViewController

 import UIKit class ViewController: UIViewController { let value: Int = 25 var percentage = Double() override func viewDidLoad() { super.viewDidLoad() // example1() // example2() // example3() // example4() } func example1() { // 1. call to a function in Utils with no argument and no return (ie suggested answer) Utils.yourfunction() // Debug messages: // Type 'Utils' has no member 'yourfunction' // Did you mean 'yourFunction'? // Did you mean 'yourFunction1'? } func example2() { // 2. call to a function in Utils with argument and no return Utils.yourfunction1(Int: Int) // Debug messages: // Type 'Utils' has no member 'yourfunction' // Did you mean 'yourFunction'? // Did you mean 'yourFunction1'? } func example3() { // 3. call to a function in Utils with argument and returning a value for percentage Utils.%() { return Int(percentage) } // Debug message: // Use of unresolved operator '.%' } func example4() { // 4. call to a function in Utils with argument and returning a value for percentage percentage = Utils.%(Int: value) -> Int { return Int(percentage) } // Debug messages: // Use of unresolved operator '.%' // Expected type before '->' // Expected type after '->' } } 

As far as I can see, the static functions Utils.yourFunction and Utils.yourFunction1 cannot be accessed from outside Utils, as shown in examples 1 and 2. And the postfix operator % apparently calls Xcode for the report Use of unresolved operator '.%' , as shown in examples 3 and 4. However, these problems may be such as I adapted the syntax used by Unniversal.

+3
source share
2 answers

Any function declared in the file area will have an implicit internal area and will be visible in the rest of the project / module.

I recommend reading the access control guide for more information.

Edit:

I'm still not sure what you are trying to do, but it looks like you are mixing global functions with static methods and custom operators.

If you want to declare a custom operator that you can use in any other project file, this solution is in the documentation that you linked in your question.

So here is what you need to declare custom% (as you defined it) for type Int:

CustomOperators.swift

 postfix operator % extension Int { static postfix func % (n: Int) -> Double { return Double(n) / 100 } } 

main.swift

 print(90%) //outputs "0.9" 

That's all. You simply declare the operator globally: postfix operator % and you define the operator function as a static method in the extension for type Int.

Now you can use your new operator in other files (for example, I did this in main.swift ).

+4
source

If you want a function to be available globally, I would recommend creating a class with static functions. for instance

 class Utils { static func yourFunction(){ //Do your stuff here } } 

To use it:

 Utils.yourfunction() 
+2
source

Source: https://habr.com/ru/post/1260700/


All Articles