Error copying files with FileManager (CFURLCopyResourcePropertyForKey failed because it was passed an URL which has no scheme)
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))
CFURLCopyResourcePropertyForKey failed because passed URL no scheme
For me this problem was fixed by adding
file://
right before the file path address like this:
var filePath = "file://\(fileURLpath)"
Swift 3: Error in copying file with FileManager
File system URLs must be created with the fileURLWithPath
initializer which adds the file://
scheme the error message complains about:
let fromURL = URL(fileURLWithPath: bundlePath)
let toURL = URL(fileURLWithPath: path)
Nevertheless there is a more convenient way to create fromURL
:
if let fromURL = Bundle.main.url(forResource: "Data", withExtension: "plist") { ...
I recommend to use generally the URL related API, for example
var docDirectory: URL {
return try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
}
var dataFileURL: URL {
return docDirectory.appendingPathComponent("Data.plist")
}
The huge benefit is you get non-optional values and get rid of a couple of guard
s.
CFURLSetResourcePropertyForKey failed when disable data backup on NSDocumentDirectory
Solved. once I changed
NSURL *fileURL = [NSURL URLWithString:localFilePath];
to
NSURL *fileURL = [NSURL fileURLWithPath:localFilePath];
everything work perfectly.
Swift: failing to copy files to a newly created folder
From the copyItemAtPath(...)
documentation:
dstPath
The path at which to place the copy ofsrcPath
. This path must
include the name of the file or directory in its new location. ...
You have to append the file name to the destination directory for thecopyItemAtPath()
call (code updated for Swift 3 and later)
let srcURL = URL(fileURLWithPath: fullElementPath)
let destURL = URL(fileURLWithPath: newMTSFolder).appendingPathComponent(srcURL.lastPathComponent)
do {
try FileManager.default.copyItem(at: srcURL, to: destURL)
} catch {
print("copy failed:", error.localizedDescription)
}
How do I save an image to a png file?
First of all never use synchronous Data(contentsOf
to load data from a server.
The error occurs because the URL has the wrong format
- A remote URL must contain a scheme and host (
http://server.com...
) - An local URL in the file system must be created with
URL(fileURLWithPath:
and the path must start with a slash.
Related Topics
Swift 3 Google Map Add Markers on Touch
Xcode Server: Opening Import File for Module 'Mobilecoreservices': Permission Denied
Swift: Make Two Types with The Same "Shape" Conform to a Common Protocol
In Swift 3.1, Unsafemutablepointer.Initialize(From:) Is Deprecated
Firebase Connection State Always Returning False on First Run
Ibdesignable View Not Rendering
Swift Cannot Invoke '*' with an Argument List of Type '(Int, Int)'
What Is The Default Value of The Padding Modifier in Swift
How Are Swift Enums Implemented Internally
Swift Playground with Debugger Support
Get The Start Point and End Point of The UIpangesturerecognizer in Swift
Converting Cmtime to String Is Wrong Value Return
How to Change Font Size of Nstableheadercell
Swift Can Change Struct Declared with Let If Using an Index But Not If Using a Loop
Create an Array of Protocols with Constrained Associated Types
Is There a Way in Swiftui to Detect If a User Has Larger Text Size Enabled