Xcode 8 gives an error at these two functions. Initializer for conditional binding must have Optional type, not 'String'. Can you help me resolve this?

// MARK: Download helper methods

// This method generates a permanent local file path to save a track to by appending
// the lastPathComponent of the URL (i.e. the file name and extension of the file)
// to the path of the app’s Documents directory.
func localFilePathForUrl(_ previewUrl: String) → URL? {
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
if let url = URL(string: previewUrl), let lastPathComponent = url.lastPathComponent {
let fullPath = documentsPath.appendingPathComponent(lastPathComponent)
return URL(fileURLWithPath:fullPath)
}
return nil
}

// This method checks if the local file exists at the path generated by localFilePathForUrl(:slight_smile:
func localFileExistsForTrack(
track: Track) → Bool {
if let urlString = track.previewUrl, let localUrl = localFilePathForUrl(urlString) {
var isDir : ObjCBool = false
if let path = localUrl.path {
return FileManager.default.fileExists(atPath: path, isDirectory: &isDir)
}
}
return false
}

@ktvmobile,
this might be a bit outdated as this was a while ago. However the error is still applicable to understand. The cryptic message suggests that you need to use a String? instead of String in the initializer.

cheers,