我尝试将NSOpenPanel
用于我的程序,但它根本不起作用,因为NSOpenpanel不会在主线程上运行。
这是我的代码
NSString *strURL;
NSOpenPanel *fileContents;
NSURL *panelURL;
NSArray *fileTypes = [NSArray arrayWithObjects:@"strings", @"STRINGS", nil];
fileContents = [NSOpenPanel openPanel];
[fileContents setCanChooseDirectories:NO];
[fileContents setCanChooseFiles:YES];
[fileContents setAllowedFileTypes:fileTypes];
[fileContents setAllowsMultipleSelection:NO];
NSInteger openPanelButton = [fileContents runModal];
if(openPanelButton == NSModalResponseOK)
{
panelURL = [fileContents URL];
strURL = panelURL.absoluteString;
}
NSArray *linesReadOnly = [strURL componentsSeparatedByString:@"\n"];
我尝试了以下两个代码:
[fileContents performSelectorOnMainThread:@selector(runModal) withObject:nil waitUntilDone:YES];
和
dispatch_sync(dispatch_get_main_queue(), ^{
//do UI stuff
});
它根本无法在主线程上实现这一点。我做错了什么?
答案 0 :(得分:0)
您可以查看以下代码。
//Open pannel to select any file or image and send it to server with upload file API
let openPannel: NSOpenPanel = NSOpenPanel()
openPannel.allowsMultipleSelection = true
openPannel.canChooseFiles = true
openPannel.canChooseDirectories = false
openPannel.runModal()
let choosenFile = openPannel.URL
if choosenFile != nil
{
let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,(choosenFile?.pathExtension!)!,
nil)
if UTTypeConformsTo((uti?.takeRetainedValue())!, kUTTypeImage) {
print("This is an image!")
}
else if UTTypeConformsTo((uti?.takeRetainedValue())!, kUTTypeFolder) {
print("This is a folder!")
}
else if UTTypeConformsTo((uti?.takeRetainedValue())!, kUTTypeAliasFile) {
print("This is a zip!")
}
else if UTTypeConformsTo((uti?.takeRetainedValue())!, kUTTypeSpreadsheet) {
print("This is a sheet!")
}
}