ios - 尝试抛出错误,但为什么呢?

标签 ios swift try-catch nsdocumentdirectory

我正在尝试从文档目录中检索图像以填充 Collection View ,但我的 try block 返回了一个 error 但我不知道是什么错误是什么或为什么会发生。

func refreshCollectionView() {
    let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
    let url = NSURL(fileURLWithPath: path)

    let filePath = url.appendingPathComponent(imagesDirectoryPath)?.path
    let fileManager = FileManager.default    

    if fileManager.fileExists(atPath: filePath!) {
        print("FILE AVAILABLE")
        do {
            titles = try FileManager.default.contentsOfDirectory(atPath: imagesDirectoryPath)
            print(titles.count)
            for image in titles {
                let data = FileManager.default.contents(atPath: imagesDirectoryPath + "/\(image)")
                let image = UIImage(data: data!)
                myImages.append(image!)
            }
            self.collectionView?.reloadData()
        } 
        catch {
            print("Error")    
        }
    }
    else {        
        print("FILE NOT AVAILABLE")    
    }    
}

print(titles.count) 从未被执行并且错误被捕获但错误是什么?

最佳答案

if fileManager.fileExists(atPath: filePath!) {
    print("FILE AVAILABLE")
    do {
        // ...
    } catch {
        print("Error:", error)
    }
}

这会告诉你实际的错误

关于ios - 尝试抛出错误,但为什么呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45521643/

相关文章:

php - 集成 PHP 脚本和 iPhone 应用程序?

swift 2 从 findObjectsInBackgroundWithBlock 解析数据

java - Try With Resources 与 Try-Catch

typescript - 如何在TypeScript中尝试catch和finally语句?

Javascript try-catch 语义(nodejs repl 中的调用堆栈)

iphone - 如何自定义 UIAlertView 消息字体?

ios - 为 iOS8 移除 "heightForRowAtIndexPath"

ios - 更新 UITextView.text 属性时未调用 textViewDidChange 方法?

objective-c - 如何在 UIIMagePickerController 中设置自定义图像

swift - 如何处理 UnsafePointer<Unmanaged<CFArray>?>