ios - In XCODE / Swift, how can I create a single function which can be called from various view controllers? -
i implement set of functions , called various view controllers in app.
whats best approach this?
an illustration of function used throughout app 1 returns formatted float string:
func roundandformatfloat(floattoreturn : float, numdecimalplaces: int) -> string{ allow formattednumber = string(format: "%.\(numdecimalplaces)f", floattoreturn) homecoming formattednumber }
i suggest creating new file called util.swift, , paste function file. util.swift like:
import uikit func roundandformatfloat(floattoreturn : float, numdecimalplaces: int) -> string{ allow formattednumber = string(format: "%.\(numdecimalplaces)f", floattoreturn) homecoming formattednumber }
you can place other functions , constants need in util.swift. phone call in view controller, this:
var str = roundandformatfloat(float, numdecimalplaces: decimalplaces)
here option. create class called util, , set function in there class function:
import uikit class util{ class func roundandformatfloat(floattoreturn : float, numdecimalplaces: int) -> string{ allow formattednumber = string(format: "%.\(numdecimalplaces)f", floattoreturn) homecoming formattednumber } }
calling this:
var str = util.roundandformatfloat(float, numdecimalplaces: decimalplaces)
you can add together other class methods here need utilize globally. note cannot create globally available variables or constants this, class variables , constants have not been implemented yet in swift.
ios xcode swift global
No comments:
Post a Comment