如何以编程方式删除plist?

时间:2011-08-15 17:37:48

标签: ios objective-c cocoa-touch plist

我想以编程方式从文件系统中删除我的整个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];
}  

4 个答案:

答案 0 :(得分:16)

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
    }
}

答案 1 :(得分:3)

这是怎么做的。

    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];

答案 2 :(得分:0)

这就是我设法删除所有旧帖子的方法,除了我当前的那个。它位于 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)")
    }
}

我认为代码可以更好,即使它的功能正常。如果有人有任何见解,请分享。

答案 3 :(得分:0)

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

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];
}