简体   繁体   English

与iOS 7的Swift 2.0兼容性

[英]swift 2.0 compatibility with iOS 7

I had my swift 1.2 code compatible with ios 7.1 to 8.4. 我的Swift 1.2代码与ios 7.1到8.4兼容。 Then when Apple released IOS 9, I migrated my code to swift 2 and my app supports now iOS 8 to 9, but will don't work with iOS 7. 然后,当Apple发布IOS 9时,我将代码迁移到swift 2,并且我的应用程序现在支持iOS 8到9,但不适用于iOS 7。

For example, The try catch is not compatible with iOS 7 and Xcode mark it as an error. 例如, try catch与iOS 7不兼容,并且Xcode将其标记为错误。

var error: NSError? 
do {
   try NSFileManager.defaultManager().createDirectoryAtPath(dataPath, withIntermediateDirectories: false, attributes: nil)
} catch let error1 as NSError {
  error = error1
}

Also I have problems with UIAlertController because xcode gives me the following label @available(iOS 8.0, *) 我也有UIAlertController的问题,因为xcode为我提供了以下标签@available(iOS 8.0, *)

how i could make compatible these two problems with iOS7? 我如何使这两个问题与iOS7兼容?

For UIAlertController , you can use the code below, but for try catch no errors shows on my XCode. 对于UIAlertController ,您可以使用下面的代码,但是要try catch ,我的XCode上不会显示任何错误。

           if #available(iOS 8.0, *) {
                let alertController = UIAlertController(title: "提示", message: hint+"不能为空", preferredStyle: .Alert)
                let OKAlert = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)
                alertController.addAction(OKAlert)
                self.presentViewController(alertController, animated: true, completion: nil)
            } else {
                let alertController: UIAlertView = UIAlertView(title: "提示", message: "不能为空", delegate: nil, cancelButtonTitle: "OK")
                alertController.show()
            }

EDIT: 编辑:

Just wondering can you try: 只是想知道您可以尝试:

if #available(iOS 8.0, *) {
do {
    let str = try NSString(contentsOfFile: "Foo.bar",
                           encoding: NSUTF8StringEncoding)
}
catch let error as NSError {
    print(error.localizedDescription)
}
}else{
#your code works on ios7
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM