Try throwing an error, but why?

时间:2017-08-05 11:59:02

标签: ios swift try-catch nsdocumentdirectory

I'm trying to retrieve images from the documents directory in order to populate a collection view but my try block returns an error but I don't know what the error is or why it is occurring.

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")    
    }    
}

The line print(titles.count) is never executed and the error is caught but what is the error?

1 个答案:

答案 0 :(得分:1)

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

This will show you actual error

相关问题