Swift 5.3 / macOS — a simple file copy method
Here’s a simple method that does three things.
- Copies a file from location ‘A’ to ‘B’
- Removes it from ‘B’ if it already exists
- Creates the necessary folder for ‘B’ if it doesn’t exist
You’ll also notice two handy little extensions and we’ll look at that too.
The two extensions fileExists
and pathComponent
are nothing but String
Extensions. Here’s fileExists
Extension String {
var fileExists: Bool {
return FileManager().fileExists(atPath: self)
}
}
It’s handy especially in applications that may be dealing a lot with files on disks.
You will also notice Config.logger.debug
Well, it’s a static usage of Apple’s brand new Logger
which you can read about here. If you’re writing new SwiftUI apps, then you’ll want to use Apple’s new Logger implementation for performance and ease-of-use.
static var logger = Logger(subsystem: "com.yourapp", category: "application")
What’s Result
you might ask — it’s just an internal implementation to hold results and comes with some custom capabilities (not relevant for this post).
Hope that helps! (This post was written in 2021and works on the latest versions of Xcode/macOS)