How to use UIAlertController() as global variable on iOS7 compatible devices?

let alertControllerLoad = UIAlertController()
let alertViewLoad = UIAlertView()

These are both established as global variables in my inbox class. I check if the device is iOS8 or above before I call UIAlertController but since it creates an instance of the UIAlertController in the class it crashes on ios7 devices. I need them to be global variables since I call them from multiple functions. Is there a work around using init() or is this something that doesnt have a work around.

Answers


Unfortunately there are no preprocessor calls in Swift, Swift could be conditionally compiled based on the evaluation of build configurations in two functions only as of today.

os() for a choice of OSX or iOS and arch() for a choice of x86_64, arm, arm64 or i386.

So you can't use the Objective-C #if/#endif conditions for checking the iOS version.

But I think you can make a trick, as you do not need to know the exact iOS version, but only if UIAlertController exists. And you can check it in the following way:

if (objc_getClass("UIAlertController") == nil) {
   // iOS 7
} else {
   // iOS 8+
}

Then you do not need to have global references to either UIAlertController or UIAlertView but instantiate them locally from inside the if and call.


For anyone else that might stumble upon this,

class Alerts {

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var iOS8Up: Bool = true

lazy var alertControllerLoad = Alerts.set()
let alertViewLoad = UIAlertView()


class func set() -> UIAlertController {

    //get the device model to know what image to post for refresh
    var device : UIDevice = UIDevice.currentDevice();
    var systemVersion = device.systemVersion;
    let numberFormatter = NSNumberFormatter()
    let systemVersionNumber = numberFormatter.numberFromString(systemVersion)
    let systemVersionFloatValue = systemVersionNumber!.floatValue

    if(systemVersionFloatValue < 8.0) {
        return UIAlertController(title: "title", message: "message", preferredStyle: UIAlertControllerStyle.Alert)
    }
    else{
        //returns with actionSheet as preferredStyle
        //will crash on anything less than iOS8
        return UIAlertController()
    }


}

}

this allows you to make a AlertController a global variable even if you're running iOS7. I spent far too long on this issue.


Need Your Help

string contains elongated words

python string character itertools

My string is: "sooo dear how areeeee youuuuuu"

Windows Forms Opacity After Shown- C#

c# winforms opacity

I am tryig to fade-in a windows form using c# but it doesnt seem to work after I have shown the form. Is it possible to change the forms opacity after Ive shown it?