iOS在objective c中使用c++完成
iOS use a c++ completion in objective C
项目示例REPOhttps://github.com/iamZoltanVaradi/PingPong
在我的应用程序中,我在c++头文件中有以下类型定义:
typedef void (*OnComplete)(const std::string &successString, const std::string &failureString);
我把它放在一个像这样的函数中。
void PingPong::requestPingPongWithBlock(const string text, OnComplete completion)
{
string successString = string();
string errorString = string();
if (text.compare("ping") == 0)
{
successString = "success ping";
}
else
{
errorString = "failure pong";
}
completion(successString, errorString);
}
在函数中被调用:
- (void)requestPingPongWithText:(NSString*)text completion:(OnComplete) compblock{
PingPong::requestPingPongWithBlock([text UTF8String],compblock);
}
但是当我这样调用它时:
[self requestPingPongWithText:ping completion:^(const std::string &successString, const std::string &failureString) {
if (!successString.empty()) {
NSLog(@"block ping");
}
else if (!failureString.empty()) {
NSLog(@"block pong");
}
}];
我得到以下错误:
不能初始化'OnComplete'类型的形参(也就是'void ')(*)(const std::string &, const std::string &)'),右值类型为'void (^)(const std::string &, const std::string &)'
如何解决这个错误?
我不知道如何在这里使用块。快速尝试对我来说效果不太好。然而,你可以使用这个(它使用我的objc_callback包装):[EDIT]如果你使用c++11的std::函数,它确实可以与块一起工作。
#include <string>
// #include <boost/function.hpp> // older c++ with boost
#include <functional> // c++11
template<typename Signature> class objc_callback;
template<typename R, typename... Ts>
class objc_callback<R(Ts...)>
{
public:
typedef R (*func)(id, SEL, Ts...);
objc_callback(SEL sel, id obj)
: sel_(sel)
, obj_(obj)
, fun_((func)[obj methodForSelector:sel])
{
}
inline R operator ()(Ts... vs)
{
return fun_(obj_, sel_, vs...);
}
private:
SEL sel_;
id obj_;
func fun_;
};
希望你能从中得到启发,如果没有-再问一次:)
// your new callback type
// boost variant:
// typedef boost::function<void(const std::string&, const std::string&)> OnComplete;
// c++11 variant
typedef std::function<void(const std::string &, const std::string &)> OnComplete;
// your test function
static void myFunc(const std::string& text, OnComplete completion)
{
NSLog(@"Try to invoke callback for %s...", text.c_str());
completion("test", "no_fail");
NSLog(@"Invoked.");
}
- (void) funCallbackWithSuccess:(const std::string&)success andFail:(const std::string&)fail
{
NSLog(@"Called with %s and %s", success.c_str(), fail.c_str());
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// objc_callback is the bridge between objective-c and c++
myFunc("soviet russia", objc_callback<
void(const std::string&, const std::string&)>(
@selector(funCallbackWithSuccess:andFail:), self ) );
// same thing but with blocks
myFunc("soviet russia", ^(const std::string& success, const std::string& fail) {
NSLog(@"Block called with %s and %s", success.c_str(), fail.c_str());
});
}
好运。
相关文章:
- 模板参数替换失败,并且未完成隐式转换
- 用C++在单独的头文件中完成函数体
- 等待整个 omp 块完成,然后再调用第二个函数
- 不必要的C++代码最终会出现在我完成的程序中吗?
- C++一个线程如何正确通信其任务已完成?
- 如何使用libav编写.mov文件,如果文件未正确完成,则可以播放
- 修复完成输入后重复"enter names of teacher"?
- async_write完成处理程序最早何时完成?
- 而不是那么多的 if 语句,我想要一个逻辑,我可以用一个语句或优化的方式来完成
- 对于完成布尔值设置为 true 后未停止的循环
- 如何完成内存分配
- 如何根据C++在同一内存位置重新初始化 C# 中的对象(还是自动完成)?
- Bash 自动完成一个选项,而不运行应用程序
- 选择排序时交换函数调用的数量和完成的交换次数是否相同?
- Qt5 - 如何将"QList<T> *"指针作为信号参数传递,并在完成后将其删除?
- C++ IDE 不会推断/自动完成对模板类中的 std::array 下标表达式的成员访问
- 链接器输入文件未使用,因为链接未完成,我无法获得.o
- CLion、MinGW 和 SDL2:进程已完成,退出代码为 -1073741515 (0xC0000135)
- 从popen()读取的输出是否在pclose()之前完成
- 如何延迟生成已完成的项目序列并对其进行迭代