swift – UIAlertView正在iOS 7上崩溃应用程序

前端之家收集整理的这篇文章主要介绍了swift – UIAlertView正在iOS 7上崩溃应用程序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我刚刚在App Store上发布了我的应用程序,但我刚收到电子邮件,并告知崩溃存在问题. @H_502_1@问题在于警报视图会在我们的应用程序出现时崩溃(仅在iOS 7中).我有一些应该修复此问题的代码,但它似乎不适用于某些版本的iOS 7.

@H_502_1@这是我目前的代码

@IBAction func resetAllButton(sender : AnyObject) {
    //If statement to check whether the modern style alert is available. Prior to iOS 8 it is not.
    if let gotModernAlert: AnyClass = NSClassFromString("UIAlertController") {

        println("UIAlertController can be instantiated")

        var alert = UIAlertController(title: "Start Over",message: "Are you sure you want to start over? This will erase your budget and all transactions.",preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "I'm sure!",style: UIAlertActionStyle.Default,handler:{ (ACTION :UIAlertAction!)in
            self.resetView()
        }))
        alert.addAction(UIAlertAction(title: "Cancel",style: UIAlertActionStyle.Cancel,handler: nil))

        self.presentViewController(alert,animated: true,completion: nil)
    }
    else {

        println("UIAlertController can NOT be instantiated")

        var alertView = UIAlertView()
        alertView.delegate = self
        alertView.title = "Start Over"
        alertView.message = "Are you sure you want to start over? This will erase your budget and all transactions."
        alertView.addButtonWithTitle("I'm sure!")
        alertView.addButtonWithTitle("Cancel")
        alertView.show()
    }
}
@H_502_1@我该怎么做才能确保我的应用程序不会在任何版本的iOS 7或8上崩溃?

我在修复版本中遇到了同样的问题.
这似乎是swift编译器的内部错误(使用Xcode 6.0.1(6A317)) @H_502_1@我用这个方法实际解决了一个objC帮助器类:

+ (BOOL) checkIfClassExists:(NSString *)className {
    id c = objc_getClass([className cStringUsingEncoding:NSASCIIStringEncoding]);
    if (c != nil) {
        return YES;
    } else {
        return NO;
    }
}
@H_502_1@用我的swift代码调用了一个桥头

if ObjCFix.checkIfClassExists("UIAlertController") {
    //iOS 8 code here
} else {
    //iOS 7 code here
}

猜你在找的Swift相关文章