C++,Object-C混合编程

C++语言 码拜 9年前 (2016-04-15) 1221次浏览
#include <Cocoa/Cocoa.h>
#include <cstdio>
#include <string>
#include <list>
static bool select_folder(std::string & foldername)
{
    foldername.clear();
    NSWindow * window = [NSWindow alloc];
    NSOpenPanel * panel = [NSOpenPanel openPanel];
    [panel setCanChooseFiles:NO];
    [panel setCanChooseDirectories:YES];
    [panel setAllowsMultipleSelection:NO];
//  [panel setMessage:@"Import one or more files or directories."];
    [panel beginSheetModalForWindow:window completionHandler:^(NSInteger result) {
        if (result == NSFileHandlingPanelOKButton)
        {
            NSURL * selection = panel.URLs[0];
            NSString * path = [selection.path stringByResolvingSymlinksInPath];
            foldername = [path cStringUsingEncoding : NSUTF8StringEncoding];
        }
    }];
    return(!foldername.empty());
}
static bool select_files(std::list<std::string> & filename_list)
{
    filename_list.clear();
    NSWindow * window = [NSWindow alloc];
    NSOpenPanel * panel = [NSOpenPanel openPanel];
    [panel setCanChooseFiles:YES];
    [panel setCanChooseDirectories:NO];
    [panel setAllowsMultipleSelection:YES];
//  [panel setMessage:@"Import one or more files or directories."];
    [panel beginSheetModalForWindow:window completionHandler:^(NSInteger result) {
        if (result == NSFileHandlingPanelOKButton)
        {
            for (int index = 0; index < panel.URLs.count; ++index)
            {
                NSURL * selection = panel.URLs[0];
                NSString * path = [selection.path stringByResolvingSymlinksInPath];
                std::string pathname = [path cStringUsingEncoding : NSUTF8StringEncoding];
                filename_list.push_back(pathname);
            }
        }
    }];
    return(!filename_list.empty());
}
int main(int argc, char * argv[])
{
    std::string foldername;
    select_folder(foldername);
    printf("folder: <%s>\n", foldername.c_str());
    std::list<std::string> filename_list;
    select_files(filename_list);
	printf("filelist:\n");
    for (std::list<std::string>::const_iterator iter = filename_list.begin(); filename_list.end() != iter; ++iter)
    {
        printf("    <%s>\n", iter->c_str());
    }
    return(0);
}

使用Object-C来实现“获取打开文件/目录”的窗口的功能,但编译有问题。(保存为.mm)

解决方案

40

能把报错贴上来么?

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C++,Object-C混合编程
喜欢 (0)
[1034331897@qq.com]
分享 (0)