Group Group Group Group Group Group Group Group Group
raywenderlich.com Forums

Video Tutorial: Saving Data in iOS Part 2: Using NSFileManager

Learn about the NSFileManager which allows you as the name suggests to manage your files.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3761-saving-data-in-ios/lessons/3

I am trying to following this tutorial using Xcode 8 and Swift 3, and have made it work with no problem, except for one thing. When I create the following function to implement the FileManagerDelegate protocol:
private func fileManager(_ fileManager: FileManager, shouldRemoveItemAt URL: URL) -> Bool {
var shouldDelete = true
let urlString = URL.absoluteString
if urlString?.range(of: “bigneder”) != nil {
shouldDelete = false
}
return shouldDelete
}
the compiler complains with the following error:
Use of undeclared type: URL’`
but it does not offer any solution to fix it. Because of this I cannot test the selective deletion. Any ideas?

If I change the URL type declaration to NSURL, the error goes away, but the delegate never gets called and all the files get deleted.

I want to clarify that for this challenge we are not copying the files from the main bundle but creating url’s and empty text files with the same names as the files in the main bundle, correct? When my newly created url’s/files in finder read 0 KB I was concerned, until I recognized we hadn’t written to disk. The difference between copying files versus creating url’s / empty files on disk with the same names as the bundle’s files is a nuance I found confusing.

I am curious, and assuming since we’re only on stage 2 that it’s coming, as to how we would copy the actual files themselves from the main bundle to disk. Great course so far. Thanks.

do not mention string as optional, check writing this way

private func fileManager(_ fileManager: FileManager, shouldRemoveItemAt URL: URL) -> Bool {
var shouldDelete = true
let urlString = URL.absoluteString
if urlString.range(of: “bigneder”) != nil {
shouldDelete = false
}
return shouldDelete
}