在我的应用程序中,我有一个 C++ 类和一个 Objective-c 类以下列方式协同工作:
乒乓球.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 "ingPong.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)sayPingid)sender {
NSString *text = @"ping";
PingPong::requestPingPongWithText([text UTF8String]);
}
// Call pong with another button
- (IBAction)sayPongid)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]]);
}
这很好用。我最终想做的是将它包装成一个带有完成 block 的函数,如下所示:
[self requestPingPongWithText: text
completion:^(NSString *successString, NSString *failureString)) completion {
if (successString) {
NSLog(@"successString: %@", successString); }
else if (failureString) {
NSLog(@"failureString: %@", failureString); }
}];
我怎样才能把我现有的代码包装成一个像上面那样的函数?
让我们在 PingPong.h 中创建一个新的 typdef:
typedef void (^OnComplete)(std::string successString, std::string failureString);
按 block 为请求创建新方法:
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)requestPingPongWithTextNSString*)text completionOnComplete) 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()]); }
}];
希望对你有帮助;)
关于c++ - 将 C++ 函数包装成 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24494361/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |