C++,Object-C混合编程

ken_scott 2016-06-15 04:25:37

#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)
...全文
121 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ken_scott 2016-06-15
  • 打赏
  • 举报
回复
编译上,已经可以了,没有夹AppKit.framework,不过不弹窗,不知道为什么
ForestDB 2016-06-15
  • 打赏
  • 举报
回复
能把报错贴上来么?

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧