ios - In XCODE / Swift, how can I create a single function which can be called from various view controllers? -


I want to apply a set of functions and that my app has to be called by different view controllers.

What is the best way to do this?

An example of a function used during the app is that returns a formatted float as a string:

  Funk round and format float (floatourtern: Float , Numberwise place: int) - & gt; String {formatted number = string (format: "%. \ (NumDecimalPlaces) f", floatToReturn) number back formatted}    

I suggest creating a new file called Util.swift, and paste that function into that file. Util.swift does the following:

  Import UIKit func Round Endformatlot (Flootroiton: Float, Numeral Success) - & gt; String {Enter the formatted number = String (Format: "%. \ (NumDecimalPlaces) f", floatToReturn) Back to the formatted number}   

You keep the other function and constant required in the. You can. To make this call in your view controller, you do this:

  var str = roundAndFormatFloat (float, numbering different location: decimal place)   

here And make another option called Utilil, and keep this function as a class function:

  import UIKit class Util {class func roundAndFormatFloat (floattoorton: float, numeric error: int) - & gt; The string {formatted number = string (format: "%. \ (NumDecimalPlaces) f", floatToReturn) returned formatted number}}   

calling will look like this:

  var str = Util.roundAndFormatFloat (float, numDecimalPlaces: decimalPlaces)   

You can add other class methods here that you need to use globally to focus Please note that you can not make available worldwide variables or constants, because square variables and constants are in swift There were also not implemented.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -