August Saint Freytag

Concept and Experience Designer, Video and Story Artist
Best in landscape.

Cocoa Drag and Drop

A brief summary and teaser text to accompany the publishing of my first technical article on Medium on the topic of building a collection view with drag and drop capabilities in the native macOS framework Cocoa/AppKit.

Read the article on Medium

While developing for an app project that is, at the time of writing, unannounced, I've often unintentionally landed on Medium, finding various articles on the use of Cocoa and its myriad of classes and modules. Quite unfortunately, most articles and more detailed documentation and examples in general are evidently written only for Cocoa Touch and, as such, only apply to iOS apps.

What I'm working with is the sparse bits of useful information painstakingly gathered from Apple's official documentation pages and, more often than I'd like, their unmaintained Objective C-era archives, loosely scattered community forum posts, the odd archived tweet and from as much as was salvageable from a few printed books on Mac app development — most considerably out of date when I found them. I thought putting together my own article on a particularly intransparent part of user interface programming would be a good push for the betterment, this is the first published piece from this line of thought.

Illustration made for the article, showing listing, selection, and dragging formation

Apple's Cocoa framework, the primary framework to build native Mac apps has more than a few issues under its hood — the same hood it uses to conceal its many hidden inner mechanics. One point of focus I came across many times is drag and drop operations, most specifically using NSCollectionView, a view class that can safely be named the cornerstone of most apps on Mac; its main purpose is presenting a collection of items with custom views, neatly arranged, resizable, and animatable. The issue at hand is trying to use multiple dragged items and wanting to define one image per item — one of the crude oversights in the framework. As is quite often the case, the exact functionality needed exists in Cocoa Touch, the iOS equivalent of Cocoa — the collection view and its related types there offer more logic that seems obviously missing on the Mac side. The article explains the technicalities of implementing drag an drop with a decent level of customisation that is at the moment not part of Cocoa while still trying to follow the base principles of Cocoa.