iOS在objective c中使用c++完成

iOS use a c++ completion in objective C

本文关键字:c++ 完成 objective iOS      更新时间:2023-10-16

项目示例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());
    });
}

好运。