在Mac OSX上打开的对话我有一个包含代码启动从C++
#ifndef PLATFORM_DEPENDENCE
#define PLATFORM_DEPENDENCE
#define USE_MAC
#include "WindowsFunctions.h"
#include "MacFunctions.h
#endif
WindowsFunctions.h头文件:
#ifndef WINTRANSFERINCLUDE
#define WINTRANSFERINCLUDE
#ifdef USE_WINDOWS
#include <string>
#include <boost/shared_ptr.hpp>
using namespace std;
#include <windows.h>
#include <Shlobj.h>
boost::shared_ptr<wstring> browseFolder();
boost::shared_ptr<vector<wstring>> getFilesInDirRecursiveImplementation(boost::shared_ptr<vector<wstring>> dirs, boost::shared_ptr<vector<wstring>> files);
#endif
#endif
和MacFunctions.h:
#ifndef MACTRANSFERINCLUDE
#define WACTRANSFERINCLUDE
#ifdef USE_MAC
#include <string>
#include <boost/shared_ptr.hpp>
using namespace std;
boost::shared_ptr<wstring> browseFolder();
boost::shared_ptr<vector<wstring>> getFilesInDirRecursiveImplementation(boost::shared_ptr<vector<wstring>> dirs, boost::shared_ptr<vector<wstring>> files);
#endif
#endif
WindowsFunctions.cpp已经实现,并且可以工作。我将如何为Mac做到这一点?我写了一个打开对话框的方法,但我不知道如何将数据返回到它在C++中调用的位置。
这是打开的对话框代码:
#import <Cocoa/Cocoa.h>
#include <string>
std::string* path() {
NSOpenPanel *op = [NSOpenPanel openPanel];
if ([op runModal] == NSOKButton) {
NSURL *nsurl = [[op URLs] objectAtIndex:0];
// nsurl.path contains the NSString I want to return as std::string
}
// ???
return something;
}
我将如何做到这一点,还是我在一个错误的方式接近问题?
这不会编译。 NSURL没有UTF8String方法。另外,他上面的API使用wstring,而不是字符串,所以你需要导出为UTF16,而不是UTF8。(它最初说[[nsurl absoluteURL] UTF8String]) – uliwitness
@uliwitness:答案已更新,但所提供的函数使用std :: string,以覆盖其他注意事项。 –
这实际上很整洁! – DonAlonzo