C++调用Object-C的方法

ken_scott 2016-06-15 04:34:34

#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);
}


C++直接调用Object-C的库的方法,保存为.mm文件,但是编译不过
...全文
168 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

29,027

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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