Home > Enterprise >  Returning contents of file as Data in Swift
Returning contents of file as Data in Swift

Time:01-30

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.

CodePudding user response:

You are mixing up NS... with native Swift and String with URL related APIs.

This is a pure Swift version preferring the URL related API

func returnGeoJsonFromDocumentsDirectory(eventId: String) -> Data {
    let documentsFolderURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
    let fileURL = documentsFolderURL.appendingPathComponent(eventId).appendingPathExtension("json")
    do {
        return try Data(contentsOf: fileURL) {
    } catch {
        print("Unable to return json", error)
        return Data()
    }
}

Or hand over the error to the caller

func returnGeoJsonFromDocumentsDirectory(eventId: String) throws -> Data {
    let documentsFolderURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
    let fileURL = documentsFolderURL.appendingPathComponent(eventId).appendingPathExtension("json")
    return try Data(contentsOf: fileURL)
}

CodePudding user response:

I figured it out. This is for anyone else who has issues with retrieving Data.

func returnGeoJsonFromDocumentsDirectory(eventId: String) -> Data{
    let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
    let url = URL(fileURLWithPath: path)
    let pathComponent = url.appendingPathComponent("\(eventId).json")
    do {
        let geoJsonFile =  try Data(contentsOf: pathComponent)
        print("Succesfully retrieved json file")
        return geoJsonFile
    }
    catch {
        print("Failed to get json file = \(error.localizedDescription)")
    }
    let emptyData = Data()
    return emptyData
}
  • Related