如何以编程方式删除 plist?

Posted

技术标签:

【中文标题】如何以编程方式删除 plist?【英文标题】:How can I delete a plist programmatically? 【发布时间】:2011-08-15 17:37:48 【问题描述】:

我想以编程方式从文件系统中删除我的整个 plist 文件。我该怎么做?

这就是我编写 plist 的方式:

+ (void)writeObjectToPList:(id)myData   
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"mobile-watchlist.plist"];
    [myData writeToFile:path atomically:YES];
  

【问题讨论】:

【参考方案1】:

NSFileManager 将允许您使用removeItemAtPath:error: 删除您的文件

+ (void)deletePList   
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"mobile-watchlist.plist"];

    NSError *error;
    if(![[NSFileManager defaultManager] removeItemAtPath:path error:&error])
    
        //TODO: Handle/Log error
    

【讨论】:

【参考方案2】:

方法如下。

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:@"mobile-watchlist.plist"];
    [fileManager removeItemAtPath: fullPath error:NULL];

【讨论】:

您不希望错误为空,您希望执行类似于 Joe 正在处理的错误(如果确实存在)的操作。【参考方案3】:

这就是我设法删除所有旧 plist 的方法,除了我现有的 plist。它在 Swift 3 中:

func removeOldPlistFiles() 

    let fileManager = FileManager.default

    // get directory with your current plist for comparison later
    let directory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    let destPath = (dir as NSString).appendingPathComponent("YourCurrentPlistName.plist")

    // Get the documents directory url
    let documentsUrl =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!

    do 

        // Get the documents directory contents urls (including subfolders urls)
        let directoryContents = try FileManager.default.contentsOfDirectory(at: documentsUrl, includingPropertiesForKeys: nil, options: [])

        // filter the directory contents and get file names
        let plistFiles = directoryContents.filter $0.pathExtension == "plist" 
        let plistFileNames = plistFiles.map $0.deletingPathExtension().lastPathComponent 

        for nameFile in plistFileNames 

            let dir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let item = (dir as NSString).appendingPathComponent("\(nameFile).plist")

            if item != self.destPath! 

                do 

                    try fileManager.removeItem(atPath: item)
                

                catch let error as NSError 

                    print("FimeManager Remove Item error: \(error.debugDescription)")
                
            
        

     catch let error as NSError 

        print("Directory Contents error: \(error.localizedDescription)")
    

我想代码可以更好,即使它是功能性的。如果有人有任何见解,请分享。

【讨论】:

【参考方案4】:

这将删除文档文件夹中的所有文件。如果应用程序在此目录中存储了您不想删除的文件,请添加代码以确保不会删除这些文件。

NSError *errorGettingAllBanners = nil;
for (NSString *file in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] error:&errorGettingAllBanners]) 
    NSError *errorDeletingAllBanners = nil;
    [[NSFileManager defaultManager] removeItemAtPath:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:file] error:&errorDeletingAllBanners];

【讨论】:

以上是关于如何以编程方式删除 plist?的主要内容,如果未能解决你的问题,请参考以下文章

如何以编程方式从 plist 添加和检索数据

在文档目录中找不到 plist 文件?如何以编程方式修改 plist?

如何在 iphone 中以编程方式创建 PLIST 文件

如何在 iPhone 中以编程方式在 plist 文件中添加多个数组

如何以编程方式访问 iOS 中其他已安装应用程序的 info.plist?

如何在 xcode 5 iOS 7 中以编程方式编辑 plist?