0

I have a "normal" swiftUI macOS project, and I need to read some "custom" files like data.dat and data.DCA.
How do I add these extensions to my Xcode project?
At the moment I use the following code to read the data, but for non standard file extensions I just get the error:

Error Domain=NSCocoaErrorDomain Code=260 "The file “test.dat” couldn’t be opened because there is no such file." UserInfo={NSFilePath=/Users/UserName/*/RandomAppName/test.dat, NSUnderlyingError=0x600001af0450 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}


       
        
        let userName = NSUserName()
        let path = "/Users/\(userName)/*/RandomAppName/test.dat" 
        var savedData = Data()
        var savedString = String()
        do {
            let url = URL(fileURLWithPath: path)
   
        savedData = try Data(contentsOf: url)
 
        if let ssavedString = String(data: savedData, encoding: .utf8) {
            savedString = ssavedString
           print(savedString)
           
        }
        } catch {
        print(error)
        }

DoTryCatch
  • 1,052
  • 6
  • 17
  • Are you really including a `*` in your file path? – jnpdx Jan 14 '21 at 23:33
  • No, of course not. I just don't wanna copy my hole file management logic on this post. It´s stored somewhere in my app. The path is working fine for every other document in there ending with txt. – DoTryCatch Jan 14 '21 at 23:37
  • The UTI shouldn't affect being able to open a certain file type in this way -- it's more for associating certain file types with the system for when they're opened via finder. I think your error suggests that you have something wrong with the path. Case sensitivity perhaps? – jnpdx Jan 15 '21 at 00:27
  • "/Users/\(userName)/*/RandomAppName/test.txt" works fine, so I think the other path should be fine as well. I can as well open the "/Users/\(userName)/*/RandomAppName/test.dat" with more in terminal. – DoTryCatch Jan 15 '21 at 00:41

0 Answers0