FREE TRIAL: Accelerate your app development career with Hacking with Swift+! >>

SOLVED: macOS Project 7, external drag isn't working

Forums > macOS


I'm doing Project 7 of Hacking with macOS and did the performExternalDrag function and there aren't any errors showing in Xcode, but when I run the app and try to drag a file from Finder into the app window, it shows the blue insertion line but when I release the mouse button, nothing happens. I've checked my code against the project files and it looks correct. Here's my code:

    func performExternalDrag(with items: [NSPasteboardItem], at indexPath: IndexPath) {
        let fm = FileManager.default

        // 1. loop over every item on the drag and drop pasteboard
        for item in items {
            // 2. pull out the string containing the URL for this item
            guard let stringURL = item.string(forType: NSPasteboard.PasteboardType(kUTTypeURL
                as String)) else { continue }

            // 3. attempt to convert the string into a real URL
            guard let sourceURL = URL(string: stringURL) else { continue }

            // 4. create a destination URL by combining 'photosDirectory' with the last path component
            let destinationURL =

            do {
                // 5. attempt to copy the file to our app's folder
                try fm.copyItem(at: sourceURL, to: destinationURL)
            } catch {
                print("Error copying item from: \(sourceURL): \(error)")

            // 6. update the array and collection view
            photos.insert(destinationURL, at: indexPath.item)
            collectionView.insertItems(at: [indexPath])



Any ideas what I missed?


I figured it out! I accidentally typed "kUTTypeURL" instead of "kUTTypeFileURL" so close but a major difference.


Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for iOS devs who want to become complete senior developers — from October 18th to 24th. Learn how to apply iOS app architecture patterns through a series of lectures and practical coding sessions.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.