Search code examples
swiftunicodensurlquicklook

NSURL subclass, with file URL, doesn't work in QuickLook, for non-ASCII filenames?


There's a lot of pieces to this, but from what I can tell, they're all necessary to reproduce the problem.

I made a trivial NSURL subclass, like so:

class URL2: NSURL { }

I made a file URL with it:

let f = URL2(fileURLWithPath: "/Users/me/Downloads/ついて.pdf")

Then I tried returning it from previewPanel(panel:previewItemAtIndex index:).

Result: I get a generic file icon in the QLPreviewPanel (but it has the correct filename).

Curiously, if I do any of these differently, it works:

  • If I use a plain NSURL(fileURLWithPath: "/Users/me/Downloads/ついて.pdf"), it displays the file contents correctly.
  • If I use an ASCII-only filename, it displays the file contents correctly.
  • If I do something else with the URL2 (like some NSFileManager operation), it locates the file just fine.

What could I be doing wrong that causes QuickLook to be unhappy with my NSURL subclass in this case?


Solution

  • Subclassing NSURL (or NSURLRequest) is often a path to madness, thanks in no small part (I think) to heavy use of NSXPC in various parts of the OS.

    I would suggest using a category with associated objects instead. This should avoid the edge case you're hitting, while still allowing you to add custom methods and properties to the NSURL objects.