-3

I am calling bellow function to get contentsof the .txt file and I am getting successfully some times but some times when I am trying to get It Is going to catch block and giving the bellow error ..please help me out

// Used for sharing

func getLogData(file: String) -> String? {

    let shareLog = dir.appendingPathComponent(file, isDirectory: false)

    if fileManager.fileExists(atPath: shareLog.path) {
        do {
            let result1 = try String(contentsOf: shareLog)
            print(result1)
            return result1
        } catch let error as NSError {
            print("Failed reading from file: \(error)")
            return nil
        }

    }

    print("Failed to get log at: \(logPath)")
    return nil
}

//Error when try to get .txt file contentsof

Error Domain=NSCocoaErrorDomain Code=264 "The file “somelog-log.txt” couldn’t be opened because the text encoding of its contents can’t be determined." UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/199D8E4D-6DFC-4327-BC57-06957BCC6EA3/Documents/somelog-log.txt}

rmaddy
  • 314,917
  • 42
  • 532
  • 579
tp2376
  • 690
  • 1
  • 7
  • 23

1 Answers1

0

Specify the encoding explicitly

let result1 = try String(contentsOf: shareLog, encoding: .utf8)
vadian
  • 274,689
  • 30
  • 353
  • 361
  • Thanks for that but when I did this .. I am getting ..... Error Domain=NSCocoaErrorDomain Code=261 "The file “2018-04-04T15/28/48-04/00-log.txt” couldn’t be opened using text encoding Unicode (UTF-8)." UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/0B8CE9C6-AAF2-41C1-9FA4-65FB57BD4B4E/Documents/2018-04-04T15:28:48-04:00-log.txt, NSStringEncoding=4} – tp2376 Apr 04 '18 at 19:59
  • Save the file always as UTF8. – vadian Apr 04 '18 at 20:29
  • I am not saving i am sharing .txt file through email.. So when I am reading the file contents it is failing with that error. – tp2376 Apr 04 '18 at 20:36
  • You are reading the file from disk so sometime it must have been written to disk. Make sure that the encoding can be determined. – vadian Apr 04 '18 at 20:40
  • txt.appendLineToURL(fileURL: logPath as URL) .. extension String { //1st method func appendLineToURL(fileURL: URL) throws { do { try (self + "\n").appendToURL(fileURL: fileURL) }catch let error { print(error) } } //2nd method func appendToURL(fileURL: URL) throws { let data = self.data(using: String.Encoding.utf8)! do { try data.append(fileURL: fileURL) }catch let error { print(error) } } } – tp2376 Apr 05 '18 at 13:28
  • And then used this for appending....extension Data { func append(fileURL: URL) throws { if let fileHandle = FileHandle(forWritingAtPath: fileURL.path) { defer { fileHandle.closeFile() } fileHandle.seekToEndOfFile() fileHandle.write(self) fileHandle.closeFile() } else { do { try write(to: fileURL, options: .atomic) print("") }catch let error { print(error) } } } } – tp2376 Apr 05 '18 at 13:40
  • Thanks for all your help.. I figure it out answer, my strings are coming as Ascii or UtF8 so I have to use both encoding techniques to read contents of file. – tp2376 Apr 15 '18 at 16:14