我发现如果文件夹被删除,您需要重新创建它,它不会自动创建它。所以我会试试这个:
let fileManager = FileManager.default
let iCloudPath = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("DirectoryName")
do {
// Try to create the file here
} catch let error {
// If that operation fails, you print the error and create the folder again
print(error.localizedDescription)
do {
try fileManager.createDirectory(atPath: iCloudPath!.path, withIntermediateDirectories: true, attributes: nil) }
catch let error {
print("Unable to create directory \(error.localizedDescription)") }
}