Error Domain=NSCocoaErrorDomain Code=256 - swift

override func viewDidLoad() {
super.viewDidLoad()
let dataURLString: String = Bundle.main.path(forResource: "IMG_0568", ofType: "JPG")!
let dataURL = URL(string: dataURLString)
do {
let binaryData = try Data(contentsOf: dataURL!, options: [])
let kbData = binaryData.subdata(in: 0..<1024)
let stringArray = kbData.map{String(format: "%02X", $0)}
let binaryString = stringArray.joined(separator: "-")
print(binaryString)
editorTextView.text = (binaryString)
} catch {
print("Failed to read the file.")
//Error Domain=NSCocoaErrorDomain Code=256 "The file “IMG_0568.JPG” couldn’t be opened." UserInfo={NSURL=/Users/..../IMG_0568.JPG}
}
I want to display the Binary Data of Image File that I have added to my Xcode project (image name: IMG_0568.JPG).
But there's the error
(Error Domain=NSCocoaErrorDomain Code=256 "The file “IMG_0568.JPG”
couldn’t be opened." UserInfo={NSURL=/Users/..../IMG_0568.JPG})
How can I fix this problem?

This is a very common mistake:
URLs in the file system must be initialized with URL(fileURLWithPath
let dataURL = URL(fileURLWithPath: dataURLString)
The difference is:
URL(fileURLWithPath expects a path starting with a slash like /Users/myUser/file.ext
URL(string expects an URL string including the scheme like file:///Users... or http://example.com
However you can avoid the mistake by using the URL related API of Bundle
let dataURL = Bundle.main.url(forResource: "IMG_0568", withExtension: "JPG")!

Related

SwiftUI macOS add costum file format (UTI)

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)
}

How to read data from a file in the App's bundle I macOS?

I'm trying to read the data from a txt file on macOS. I use String(contentsOf:) and Bundle.main.path(forResource:) like I would on iOS. However, this doesn't work on macOS. I have tried several solutions from other posts but because of macOS they don't seem to work.
Thanks in advance.
Edit:
My code:
let path = Bundle.main.path(forResource: "file", ofType: "txt")! // no error
let url = URL(string: path)! // no error
let contents: String
do {
contents = try String(contentsOf: url)
} catch {
print(error) // Error Domain=NSCocoaErrorDomain Code=262 "The file couldn’t be opened because the specified URL type isn’t supported."
contents = "ERROR"
}
print(contents)
I can save the data to a folder on disk, but I want to ship this file with my app.
URL(string is the wrong API. It's only for URL strings starting with a scheme (http://, file://)
For file system paths you have to use URL(fileURLWithPath.
But in this situation there is a much better way
Replace
let path = Bundle.main.path(forResource: "file", ofType: "txt")!
let url = URL(string: path)! // no error
with
let url = Bundle.main.url(forResource: "file", withExtension: "txt")

Error copying files with FileManager (CFURLCopyResourcePropertyForKey failed because it was passed an URL which has no scheme)

I'm trying to copy some (media) files from one folder to another using FileManager's copyItem(at:path:), but I'm getting the error:
CFURLCopyResourcePropertyForKey failed because it was passed an URL which has no scheme
Error Domain=NSCocoaErrorDomain Code=262 "The file couldn’t be opened because the specified URL type isn’t supported."
I'm using Xcode 9 beta and Swift 4.
let fileManager = FileManager.default
let allowedMediaFiles = ["mp4", "avi"]
func isMediaFile(_ file: URL) -> Bool {
return allowedMediaFiles.contains(file.pathExtension)
}
func getMediaFiles(from folder: URL) -> [URL] {
guard let enumerator = fileManager.enumerator(at: folder, includingPropertiesForKeys: []) else { return [] }
return enumerator.allObjects
.flatMap {$0 as? URL}
.filter { $0.lastPathComponent.first != "." && isMediaFile($0)
}
}
func move(files: [URL], to location: URL) {
do {
for fileURL in files {
try fileManager.copyItem(at: fileURL, to: location)
}
} catch (let error) {
print(error)
}
}
let mediaFilesURL = URL(string: "/Users/xxx/Desktop/Media/")!
let moveToFolder = URL(string: "/Users/xxx/Desktop/NewFolder/")!
let mediaFiles = getMediaFiles(from: mediaFilesURL)
move(files: mediaFiles, to: moveToFolder)
The error occurs because
URL(string: "/Users/xxx/Desktop/Media/")!
creates a URL without a scheme. You can use
URL(string: "file:///Users/xxx/Desktop/Media/")!
or, more simply,
URL(fileURLWithPath: "/Users/xxx/Desktop/Media/")
Note also that in fileManager.copyItem() the destination must
include the file name, and not only the destination
directory:
try fileManager.copyItem(at: fileURL,
to: location.appendingPathComponent(fileURL.lastPathComponent))

Load XML file from main Bundle in Swift 3.0

I have a .GPX file contains routing info of a hiking trip which I want to load into my app. Everything is ok if I load it from remote URL (https://dl.dropboxusercontent.com/u/45741304/appsettings/Phu_si_Lung_05_01_14.gpx) but I can't load this same file from app bundle (already in "Copy bundle resources" and had correct target membership).
Here's my code for loading this file from remote URL:
var xmlParser: XMLParser!
func startParsingFileFromURL(urlString: String) {
guard let url = URL(string: urlString) else {
print("Can't load URL: \(urlString)")
return
}
self.xmlParser = XMLParser(contentsOf: url)
self.xmlParser.delegate = self
let result = self.xmlParser.parse()
print("parse from URL result: \(result)")
if result == false {
print(xmlParser.parserError?.localizedDescription)
}
}
and from the main bundle:
func startParsingFile(fileName: String, fileType: String) {
guard let urlPath = Bundle.main.path(forResource: fileName, ofType: fileType) else {
print("Can't load file \(fileName).\(fileType)")
return
}
guard let url:URL = URL(string: urlPath) else {
print("Error on create URL to read file")
return
}
self.xmlParser = XMLParser(contentsOf: url)
self.xmlParser.delegate = self
let result = self.xmlParser.parse()
print("parse from file result: \(result)")
if result == false {
print(xmlParser.parserError?.localizedDescription)
}
}
Error on load from app bundle:
parse from file result: false
Optional("The operation couldn’t be completed. (Cocoa error -1.)")
You are saying:
guard let urlPath = Bundle.main.path(forResource: fileName, ofType: fileType) else {
print("Can't load file \(fileName).\(fileType)")
return
}
guard let url:URL = URL(string: urlPath) else {
print("Error on create URL to read file")
return
}
First, it is very silly to turn a string path into a URL. You knew you wanted a URL, so why didn't you start by calling url(forResource:...)?
Second, if you ever do turn a string path into a URL, you must make a file URL.

Able to write/read file but unable to delete file SWIFT

I store an .jpg image i the iOS documents directory. I can write files and read files but when it comes to deleting them it says that there is no such file but that cannot be because I can read it with the same url.
Reading:
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let path = NSURL(fileURLWithPath: paths[0] as String)
let fullPath = path.appendingPathComponent(info["pi"] as! String)
let data = NSData(contentsOf: fullPath!)
Deleting:
let fileManager = FileManager.default
fileManager.delegate = self
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let path = NSURL(fileURLWithPath: paths[0] as String)
let fullPath = path.appendingPathComponent(info["pi"] as! String)
do {
try fileManager.removeItem(atPath: "\(fullPath!)")
} catch {
print("\(error)")
}
It throws:
Error Domain=NSCocoaErrorDomain Code=4 "“image_496251232.806566.jpg” couldn’t be removed." UserInfo={NSUnderlyingError=0x1758eb40 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}, NSFilePath=file:///var/mobile/Containers/Data/Application/269ADA58-6B09-4844-9FAA-AC2407C1D991/Documents/image_496251232.806566.jpg, NSUserStringVariant=(
Remove
)}
Your fullPath variable is a (optional) URL. To convert that to a file path string, use the .path property, not string interpolation:
fileManager.removeItem(atPath: fullPath!.path)
Or better, use the URL directly without converting it to a path:
fileManager.removeItem(at: fullPath!)
(And get rid of the forced unwrapping in favor of option binding ... :-)