I like to place often used methods in a separate file. I found this answer Use function from one class in another class in Swift but I get errors using it the way I want.
Say i want to create a method called msgBox that pops up an alert box. I created a separate empty Swift file and place this code in it.
import UIKit
class Utils: UIViewController {
class func msgBox (titleStr:String = "Untitled", messageStr:String = "Alert text", buttonStr:String = "Ok") {
var alert = UIAlertController(title: titleStr, message: messageStr, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: buttonStr, style: .Default, handler: { (action) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
}))
self.presentViewController(alert, animated: true, completion: nil)
}
}
I want to call it like this, but I get errors doing so. Does anyone know what I'm doing wrong?
Utils.msgBox(titleStr: "Hello!", messageStr: "Are you sure?")