2015-09-23 42 views
0

我的swift 1.2代码与ios 7.1至8.4兼容。然后,当Apple发布IOS 9时,我将代码迁移到swift 2,而我的应用程序现在支持iOS 8至9,但不会与iOS 7配合使用。Swift 2.0与iOS 7的兼容性

例如,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 
} 

而且我有UIAlertController问题,因为xcode中给了我下面的标签@available(iOS 8.0, *)

我怎么能让兼容这两个问题与iOS7?

回答

0

对于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() 
      } 

编辑:

只是想知道,你可以尝试:

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 
} 
+0

你用什么版本的Xcode模拟的iOS 7?我已经安装了iOS 8-9和Xcode 6.4到iOS 7.1的Xcode 7,但这最后显示了我很多错误。 – corocraft

+0

不,我不能安装IOS7。你知道ios7是非常少量的用户,也许你可以忽略它。或者尝试我的编辑。 –