NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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      

Hacking with Swift is sponsored by MadMachine

SPONSORED Want to explore your Swift skill outside of the Apple world? Join the MadMachine community and start to program microcontrollers in Swift.

Get it now

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.