Swift 我试图删除存储在documents目录中的map上的pin,但在文件“;保存位置”;不能’;不能打开

Swift 我试图删除存储在documents目录中的map上的pin,但在文件“;保存位置”;不能’;不能打开,swift,xcode,Swift,Xcode,我正在尝试删除地图上的一个pin,我在地图上有几个pin存储在documents目录中。我想一次只删除一个pin,但调用delete方法后,我出现此错误-无法打开文件“SavedLocations”。如何删除文档目录中保存的单个pin?请帮忙,谢谢 func getDocumentsDirectory() -> URL { let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMas

我正在尝试删除地图上的一个pin,我在地图上有几个pin存储在documents目录中。我想一次只删除一个pin,但调用delete方法后,我出现此错误-无法打开文件“SavedLocations”。如何删除文档目录中保存的单个pin?请帮忙,谢谢

func getDocumentsDirectory() -> URL {
    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    return paths[0]
}
//Load method
func loadData() {
    print("loaded data")
    let filename = getDocumentsDirectory().appendingPathComponent("SavedLocations")

    do {
        let data = try Data(contentsOf: filename)
        locations = try JSONDecoder().decode([CodableMKPointAnnotation].self, from: data)

    } catch {
        print("Unable to load saved data")
    }
}
//Save method
func saveData() {
    print("saved data")

    do {
        let filename = getDocumentsDirectory().appendingPathComponent("SavedLocations")
       // print(filename)
        let data = try JSONEncoder().encode(self.locations)
        try data.write(to: filename, options: [.atomicWrite, .completeFileProtection])

    } catch {
        print("Unable to save data")
    }

}
//Delete method
func delete() {

    let fileManager = FileManager.default
    let filename = getDocumentsDirectory().appendingPathComponent("SavedLocations")
    print("Pin deleted")
    print(filename)

    do {
        let items = try fileManager.contentsOfDirectory(at: filename, includingPropertiesForKeys: .none, options: .skipsHiddenFiles)
        print(items)

        for item in items {
            try fileManager.removeItem(at: item)
            print(item)
        }

    } catch let error {
        print("\(error.localizedDescription)")
    }
 }
将此方法更改为:

func delete(){
让fileManager=fileManager.default
让filename=getDocumentsDirectory().appendingPathComponent(“SavedLocations”)
打印(“Pin已删除”)
打印(文件名)
做{
let items=try fileManager.contentsOfDirectory(位于:filename,includingPropertiesForKeys:.none,options:.skipsHiddenFiles)
打印(项目)
如果(newArray.count>0){
请尝试fileManager.removeItem(位于:项[0])
}
}捕捉错误{
打印(“\(错误。本地化描述)”)
}
}