objective-c - How to get the file path when dragging a file under OS X
迷茫
迷茫 2017-05-02 09:28:28
0
1
597

Drag the file to get the file path

// (id <NSDraggingInfo>)sender
NSURL *fileURL=[NSURL URLFromPasteboard: [sender draggingPasteboard]];

Get the URL and print 2016-08-01 17:11:21.201 DragAndDrop[3504:170407] file:///.file/id=6571367.14796550. Instead of a file path in the format of /Users/jolie/Downloads.

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(1)
阿神
NSString *filePath = [fileURL path];

Reflection on the problem: This problem still took a lot of time. My initial idea was to get NSURL from Pasteboard. NSImage can also be initialized through Pasteboard, so I wanted to get fileName from Pasteboard. Since fileName is of string type, I wanted to get NSString. Initialized through Pasteboard, but not found. From the Apple API, we can see that NSString follows the NSPasteboardWriting and NSPasteboardReading protocols, but does not provide relevant interfaces.
However, a project pulled from github found that the answer to the problem is very simple. NSURL provides path to obtain the file path. The reason why the problem took so much time can only be said to be that I am not familiar with the NSURL API.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template