For the first time, I am trying to return a data file from documents. I am getting an error: No exact matches in call to initializer
Here's my code to retrieve the data file:
func returnGeoJsonFromDocumentsDirectory(eventId: String) -> Data{
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
let filePath = paths.appendingPathComponent("\(eventId).json")
let geoJsonFile = Data(contentsOfFile: filePath)
if geoJsonFile != nil {
return geoJsonFile!
}
else {
print("Unable to return json")
let emptyData = Data()
return emptyData
}
If I use NSData, this seems to solve the issue but I was under the impression you shouldn't NSData in Swift.
You are mixing up
NS...with native Swift andStringwithURLrelated APIs.This is a pure Swift version preferring the URL related API
Or hand over the error to the caller
In iOS 16+, macOS 13+ there are new APIs to get the system folders in a sandboxed environment