OGeek|极客世界-中国程序员成长平台

标题: c++ - iOS 在 Objective-C 中使用 c++ 完成 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:38
标题: c++ - iOS 在 Objective-C 中使用 c++ 完成

示例项目 repo https://github.com/iamZoltanVaradi/PingPong

在我的应用程序中,我在 c++ header 中有以下 typedef:

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)requestPingPongWithTextNSString*)text completionOnComplete) 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");
   }
}];

我收到以下错误:

cannot initialize a parameter of type 'OnComplete' (aka 'void (*)(const std::string &, const std::string &)') with an rvalue of type 'void (^)(const std::string &, const std::string &)'

我该如何解决这个错误?



Best Answer-推荐答案


我不确定您如何在这里使用积木。快速尝试对我来说效果不太好。但是你可以使用它(它使用我的 objc_callback 包装器): [编辑] 如果您使用 c++11 的 std::function,它确实适用于 block 。请参阅下面的代码。

#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) funCallbackWithSuccessconst std::string&)success andFailconst  std::string&)fail
{
    NSLog(@"Called with %s and %s", success.c_str(), fail.c_str());
}

- (BOOL)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)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++ - iOS 在 Objective-C 中使用 c++ 完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24510622/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4