gpt4 book ai didi

ios - 在ios swift中覆盖文本文件

转载 作者:搜寻专家 更新时间:2023-11-01 05:52:27 24 4
gpt4 key购买 nike

我在应用程序开始时从 Internet 下载文件,然后将其保存在本地并使用其数据。我希望下载的文件在开始时每次都覆盖以前的文件,但是我无法获取覆盖的数据,它一直显示以前的文件。如果我通过检查它是否存在来删除它,那么它会给出一个错误“无法复制到“文档”,因为同名的项目已经存在。”检查它的存在然后它给出这个错误:

"Error took place while reading from file. Error description: %@ The file “Splashk.text” couldn’t be opened because there is no such file."

这是我的代码:

// checking file existence
do{
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let url = URL(fileURLWithPath: path)
let filePath = url.appendingPathComponent("Splashk.text").path
let fileManager1 = FileManager.default
if fileManager1.fileExists(atPath: filePath) { // if available, delete the file and re create an empty file
print("FILE AVAILABLE")
try fileManager1.removeItem(atPath: filePath)
if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let path = dir.appendingPathComponent("Splashk.text")
// writing
do {
try text.write(to: path, atomically: true, encoding: String.Encoding.utf8)
}
catch {/* error handling here */}
}
} else { // if not available, create an empty file
print("FILE NOT AVAILABLE")
if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let path = dir.appendingPathComponent("Splashk.text")
// writing
do {
try text.write(to: path, atomically: true, encoding: String.Encoding.utf8)
}
catch {/* error handling here */}
}
}
}
catch let error as NSError {
print("An error took place: \(error)")
}
// getting the file path for destination file
let documentsUrl:URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first as URL!//try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true) as URL!
let destinationFileUrl = documentsUrl.appendingPathComponent("Splashk.text")

let empId = self.defaults.object(forKey: "EmpId") as! String
// fileURL got the online url from which file is getting downloaded
let fileURL = URL(string:(defaults.object(forKey: "MainAddress") as! String).appending(download url)

let sessionConfig = URLSessionConfiguration.default
let session1 = URLSession(configuration: sessionConfig)

let request = URLRequest(url:fileURL!)
let task1 = session1.downloadTask(with: request) { (tempLocalUrl, response, error) in
if let tempLocalUrl = tempLocalUrl, error == nil {
// Success
if let statusCode = (response as? HTTPURLResponse)?.statusCode {
print("Successfully downloaded. Status code: \(statusCode)") // it is 200
}
do {
print("temp local url \(tempLocalUrl)")
try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)
let fileManager = FileManager.default
// Check if file exists
} catch (let writeError) {
print("Error creating a file \(destinationFileUrl) : \(writeError)") //
}
} else {
print("Error took place while downloading a file. Error description: %@", error?.localizedDescription);
}
}
task1.resume()

//reading back from the destination file
do {
str = try String(contentsOf: destinationFileUrl, encoding: String.Encoding.utf8) as NSString
print("file text = \(str)")
parseXML()
} catch {/* error handling here */
print("Error took place while reading from file. Error description: %@", error.localizedDescription)
}

最佳答案

试试这行代码,我正在使用它并且有效

let fileManager = FileManager.default
let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
let filePath = documentDirectoryPath.appendingPathComponent("Splashk.text")
if fileManager.fileExists(atPath: filePath){
do{
try fileManager.removeItem(atPath: filePath)
}catch let error {
print("error occurred, here are the details:\n \(error)")
}
}

关于ios - 在ios swift中覆盖文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44844633/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com