我正在一个个人项目中,尝试将从应用程序内购买中下载的文件移动到自定义文件夹内的用户文档目录中。我首先要从“目录”文件夹中提取内容,并将其放置在新文件夹中。第一部分工作正常,我可以选择目录文件夹。我的问题是我无法提取此文件夹中的内容,并且收到错误消息:
无法读取内容:Error Domain = NSCocoaErrorDomain代码= 260“文件夹“ Contents”不存在。” UserInfo = {NSFilePath = Contents,NSUserStringVariant =( 夹 ),NSUnderlyingError = 0x1cc056470 {Error Domain = NSPOSIXErrorDomain代码= 2“没有这样的文件或目录”}}
func process(completion: @escaping(Bool, AppError?) -> Void) {
let fm = FileManager.default
var files: [String]!
do {
files = try fm.contentsOfDirectory(atPath: url.path)
print("files: \(files)")
} catch {
print("error: finding zip URL:", error.localizedDescription)
completion(false, AppError.processDownload)
}
// Creates the directory if it doesn't exist - Works!
guard checkOrCreateAssetDirectory() == true else { return completion(false, AppError.processNewPurchase) }
let appdir = appDelegate.getDocumentsDirectory()
let newPath = appdir.path + "/Assets/"
for file in files {
print("Downloaded file: \(file)")
if file == "Contents" {
do {
// Error happens on this line
let folderContents = try fm.contentsOfDirectory(atPath: file)
for content in folderContents {
do {
try fm.moveItem(atPath: file, toPath: newPath)
print("Moving file: \(file) to: \(newPath)")
completion(true, nil)
} catch {
print("error: \(error)")
completion(false, AppError.processDownload)
}
}
} catch {
print("Contents could not be read: \(error)")
}
}
}
}