如何在我的应用程序ios swift中删除存储的数据?

时间:2016-01-07 07:29:56

标签: ios swift download swift2 ios9

您好我的应用程序中存储了一些数据。 这是我下载数​​据的代码

func downloadShow(slug: String, show: NSDictionary) {

    SVProgressHUD.showWithStatus("Downloading...")
    let sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration()
    let session = NSURLSession(configuration: sessionConfig, delegate: nil, delegateQueue: nil)
    let url = NSURL(string: show["file"] as! String)
    let request = NSMutableURLRequest(URL: url!)
    request.HTTPMethod = "GET"
    let task = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
        if (error == nil) {
            let showFileName = url?.lastPathComponent
            let programMP3Path = self.localURL + "/" + slug + "/" + showFileName!
            let programDataPath = programMP3Path + ".dat"
            data?.writeToFile(programMP3Path, atomically: true)
            show.writeToFile(programDataPath, atomically: true)
            print("Success")
           SVProgressHUD.dismiss()
        }
        else {
            // Failure
            print("Faulure: \(error)");
        }
    })
    task.resume()
}

现在我想清除点击删除按钮的特定数据。那我怎么能删除这些数据?

这是我存储数据的路径

/var/mobile/Containers/Data/Application/57EDF9A1-1108-4BA9-AE0A-57B8BA61869A/Documents/

2 个答案:

答案 0 :(得分:1)

你可以使用removeItemAtURL

let fileManager = NSFileManager.defaultManager()
let enumerator = fileManager.enumeratorAtURL(urlDir, includingPropertiesForKeys: nil, options: nil, errorHandler: nil)
while let file = enumerator?.nextObject() as? String {
    fileManager.removeItemAtURL(urlDir.URLByAppendingPathComponent(file), error: nil)
}

答案 1 :(得分:0)

使用文件管理器删除你的文件,我添加了obj-c Code,

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
BOOL success = [fileManager removeItemAtPath:YourFilePath error:&error];