Swift 5.3 / macOS — a simple file copy method

Swift for the Slow
Feb 14 · 1 min read

Here’s a simple method that does three things.

  1. Copies a file from location ‘A’ to ‘B’
  2. Removes it from ‘B’ if it already exists
  3. 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)

Mac O’Clock

The best stories for Apple owners and enthusiasts

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store