在 XCODE / Swift 中,如何创建可以从各种视图控制器调用的单个函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26264786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In XCODE / Swift, how can I create a single function which can be called from various view controllers?
提问by user3535074
I would like to implement a set of functions and that they be called by various view controllers in my app.
我想实现一组函数,并由我的应用程序中的各种视图控制器调用它们。
Whats the best approach for this?
最好的方法是什么?
An example of a function to be used throughout the app is this one which returns a formatted Float as a string:
在整个应用程序中使用的一个函数示例是这个函数,它返回一个格式化的 Float 作为字符串:
func roundAndFormatFloat(floatToReturn : Float, numDecimalPlaces: Int) -> String{
let formattedNumber = String(format: "%.\(numDecimalPlaces)f", floatToReturn)
return formattedNumber
}
回答by DrOverbuild
I suggest creating a new file called Util.swift, and paste that function into that file. This is what Util.swift would look like:
我建议创建一个名为 Util.swift 的新文件,并将该函数粘贴到该文件中。这是 Util.swift 的样子:
import UIKit
func roundAndFormatFloat(floatToReturn : Float, numDecimalPlaces: Int) -> String{
let formattedNumber = String(format: "%.\(numDecimalPlaces)f", floatToReturn)
return formattedNumber
}
You can place other functions and constants you need in Util.swift. To call it in your view controller, you do this:
您可以在 Util.swift 中放置您需要的其他函数和常量。要在视图控制器中调用它,请执行以下操作:
var str = roundAndFormatFloat(float, numDecimalPlaces: decimalPlaces)
Here is another option. Create another class called Util, and put this function in there as a class function:
这是另一种选择。创建另一个名为 Util 的类,并将此函数作为类函数放入其中:
import UIKit
class Util{
class func roundAndFormatFloat(floatToReturn : Float, numDecimalPlaces: Int) -> String{
let formattedNumber = String(format: "%.\(numDecimalPlaces)f", floatToReturn)
return formattedNumber
}
}
Calling it would look like this:
调用它看起来像这样:
var str = Util.roundAndFormatFloat(float, numDecimalPlaces: decimalPlaces)
You can add other class methods here that you need to use globally. Note that you cannot create globally available variables or constants this, as class variables and constants have not been implemented yet in Swift.
您可以在此处添加其他需要全局使用的类方法。请注意,您不能创建全局可用的变量或常量 this,因为类变量和常量尚未在 Swift 中实现。
回答by Ovi Bortas
The Swift way is to extend UIViewController
and add your functions there.
Swift 的方法是在UIViewController
那里扩展和添加你的函数。
Swift 3 Example:
Swift 3 示例:
extension UIViewController {
func roundAndFormatFloat(floatToReturn : Float, numDecimalPlaces: Int) -> String{
let formattedNumber = String(format: "%.\(numDecimalPlaces)f", floatToReturn)
return formattedNumber
}
func foo() -> String {
return "foo"
}
}
回答by Anil Gupta
Creating Util class in Swift:
在 Swift 中创建 Util 类:
- Create new file with name Util
- Create a class functions : class func funtionname:() -> {}
- 创建名为 Util 的新文件
- 创建一个类函数:class func funtionname:() -> {}
Exmaple:
例子:
import UIKit import Foundation
导入 UIKit 导入基础
class Util: NSObject {
//MARK: - SET USER LOGGED IN
class func set_IsUserLoggedIn(state : Bool) {
var userloggedin: UserDefaults?
userloggedin = UserDefaults.standard
userloggedin?.set(state, forKey: "IS_USER_LOGGED_IN")
}
//MARK: - GET USER LOGGED IN
class func get_IsUserLoggedIn() -> Bool {
var userloggedin: UserDefaults?
userloggedin = UserDefaults.standard
var checkUser:Bool? = false
checkUser = userloggedin?.bool(forKey: "IS_USER_LOGGED_IN")
return checkUser!
}
}
You can call the Util function in Any Class file like:
您可以在 Any Class 文件中调用 Util 函数,例如:
if Util.get_IsUserLoggedIn(){
print("User is logged in")
}
else{
print("User is not logged in")
}