WWDC23 SALE: Save 50% on all my Swift books and bundles! >>

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?

1      

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

1      

Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.