• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

c++ - iOS 在 Objective-C 中使用 c++ 完成

[复制链接]
菜鸟教程小白 发表于 2022-12-12 10:38:30 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

示例项目 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap