将C ++函数包装成块

时间:2014-06-30 16:03:43

标签: c++ ios objective-c objective-c-blocks

在我的应用程序中,我有一个C ++类和一个Objective-c类,它们以下列方式协同工作:

PingPong.h

#ifndef __RSSCPPCallbackTest__PingPong__h
#define __RSSCPPCallbackTest__PingPong__h

#include <iostream>

class PingPong
{
public:
    static void requestPingPongWithText(std::string text);
    static void eventRequestPingPongWithTextSuccess(std::string successString);
    static void eventRequestPingPongWithTextFailure(std::string failureString);
};

#endif

PingPong.cpp

#ifndef __RSSCPPCallbackTest__PingPong__m
#define __RSSCPPCallbackTest__PingPong__m

#include "PingPong.h"

void PingPong::requestPingPongWithText(std::string text)
{
    if (text.compare("ping") == 0)
    {
        PingPong::eventRequestPingPongWithTextSuccess("success ping");
    }
    else
    {
        PingPong::eventRequestPingPongWithTextFailure("failure pong");
    }
}

#endif

Objective-C类:MainViewController.mm

@implementation MainViewController

        /* init, viewDidLoad, etc... */

        // Call ping with a button
        - (IBAction)sayPing:(id)sender {

            NSString *text = @"ping";

            PingPong::requestPingPongWithText([text UTF8String]);
        }

        // Call pong with another button
        - (IBAction)sayPong:(id)sender {

            NSString *text = @"pong";

            PingPong::requestPingPongWithText([text UTF8String]);
        }    

@end

        void PingPong::eventRequestPingPongWithTextSuccess(std::string successString)
        {
            NSLog(@"successString: %@", [NSString stringWithCString:successString.c_str()
                                                           encoding:[NSString defaultCStringEncoding]]);
        }
        void PingPong::eventRequestPingPongWithTextFailure(std::string failureString)
        {
            NSLog(@"failureString: %@", [NSString stringWithCString:failureString.c_str()
                                                           encoding:[NSString defaultCStringEncoding]]);
        }

这很好用。我最后要做的是将它包装成一个带有完成块的函数,如下所示:

    [self requestPingPongWithText: text
    completion:^(NSString *successString, NSString *failureString)) completion {
     if (successString) {
NSLog(@"successString: %@", successString); } 
    else if (failureString) { 
NSLog(@"failureString: %@", failureString); }
    }];

如何将现有代码包装起来,使其具有如上所示的功能?

1 个答案:

答案 0 :(得分:2)

让我们在PingPong.h中创建一个新的typdef:

typedef void (^OnComplete)(std::string successString, std::string failureString);

按块创建请求的新方法:

static void requestPingPongWithBlock(std:string text, OnComplete block);

另一种方式,明确:

static void requestPingPongWithBlock(std:string text, void (^block)(std::string successString, std::string failureString));

在PingPong.cpp中实现:

void PingPong::requestPingPongWithBlock(std::string text, OnComplete block)
{
    if (text.compare("ping") == 0)
    {
        block("success ping", "");
    }
    else
    {
        block("", "failure pong");
    }   
}
MainViewController.mm中的

-(void)requestPingPongWithText:(NSString*)text completion:(OnComplete) compblock{
    PingPong::requestPingPongWithBlock([text UTF8String],compblock);
}

你可以这样称呼它:

[self requestPingPongWithText: text
                   completion:^(std::string successString, std::string failureString) {
                       if (successString.length() != 0) {
                           NSLog([NSString stringWithUTF8String: successString.c_str()]); }
                       else if (failureString.length() != 0) {
                           NSLog([NSString stringWithUTF8String: failureString.c_str()]); }
                   }];

我希望它有用;)