hoc*_*man 4 macos xcode cocoa drag-and-drop objective-c
我正在实现我的拖放方法.我需要当用户在我的应用程序窗口中拖动某些内容时,我可以获取该文件URL.NSURL
需要转换为char
.没关系.但是如何file://
从网址中删除?我目前的代码:
pboard = [sender draggingPasteboard];
NSString *url = [[NSURL URLFromPasteboard:pboard] absoluteString];
input_imageN = strdup([url UTF8String]);
Run Code Online (Sandbox Code Playgroud)
它确定,但它给url添加file://
前缀.我试过用
NSURL *fileUrl = [[NSURL URLFromPasteboard:pboard] isFileURL];
NSString *url = [fileUrl absoluteString];
NSLog(@"url: %@", [NSURL URLFromPasteboard:pboard]);
input_imageN = strdup([url UTF8String]);
Run Code Online (Sandbox Code Playgroud)
但它说
Cannot initialize a variable of type 'NSURL *' with an rvalue of type 'BOOL' (aka 'signed char')
Run Code Online (Sandbox Code Playgroud)
在
NSURL *fileUrl = [[NSURL URLFromPasteboard:pboard] isFileURL];
Run Code Online (Sandbox Code Playgroud)
小智 16
要在文件系统的相应表示形式中将文件URL作为C字符串转到路径,您需要执行以下操作:
NSURL *fileURL = [NSURL URLFromPasteboard: pboard];
NSString *filePath = [fileURL path];
char *filesystemRepresentation = [filePath filesystemRepresentation];
Run Code Online (Sandbox Code Playgroud)
这避免了剥离方案的假设只留下了路径,或者文件系统肯定很乐意接受UTF8编码的路径.
url = [url stringByReplacingOccurencesOfString:@"file://" withString:@""];
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。干杯!