声明的括号块是Swift 2中未使用的闭包

时间:2015-09-16 09:16:11

标签: ios swift swift2

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)

if let directoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as? NSURL {

    let urls = try NSFileManager.defaultManager().contentsOfDirectoryAtURL(directoryURL, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions.SkipsHiddenFiles)
    //Error: Errors thrown from here are not handled

    catch let error as NSError {
    //Error: Consecutive statements on a line must be separated by ';'
    //Error: Braced block of statements is an unused closure
    //Error: Expected expression

        print("There's an error")
    }

    downloadedPhotoURLs = urls as [NSURL]
    collectionView!.reloadData()
}
}

这个错误非常奇怪,try / catch模式似乎没有任何问题,因为它在我的其他项目中运行良好,但我不知道Xcode为什么不能识别它。

1 个答案:

答案 0 :(得分:1)

您忘记使用do

if let directoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as? NSURL {
    do {
        let urls = try NSFileManager.defaultManager().contentsOfDirectoryAtURL(directoryURL, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions.SkipsHiddenFiles)
        downloadedPhotoURLs = urls as [NSURL]
        collectionView!.reloadData()
    } catch let error as NSError {
        print(error)
    }
}