UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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

Forums > macOS

Hi

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 =
                photosDirectory.appendingPathComponent(sourceURL.lastPathComponent)

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

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

        }

    }

Any ideas what I missed?

3      

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

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.