c++ - 将 C++ 函数包装成 block
<p><p>在我的应用程序中,我有一个 C++ 类和一个 Objective-c 类以下列方式协同工作:</p>
<p>乒乓球.h</p>
<pre><code>#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
</code></pre>
<p>PingPong.cpp</p>
<pre><code>#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
</code></pre>
<p>Objective-C 类:MainViewController.mm</p>
<pre><code>@implementation MainViewController
/* init, viewDidLoad, etc... */
// Call ping with a button
- (IBAction)sayPing:(id)sender {
NSString *text = @"ping";
PingPong::requestPingPongWithText();
}
// Call pong with another button
- (IBAction)sayPong:(id)sender {
NSString *text = @"pong";
PingPong::requestPingPongWithText();
}
@end
void PingPong::eventRequestPingPongWithTextSuccess(std::string successString)
{
NSLog(@"successString: %@", [NSString stringWithCString:successString.c_str()
encoding:]);
}
void PingPong::eventRequestPingPongWithTextFailure(std::string failureString)
{
NSLog(@"failureString: %@", [NSString stringWithCString:failureString.c_str()
encoding:]);
}
</code></pre>
<p>这很好用。我最终想做的是将它包装成一个带有完成 block 的函数,如下所示:</p>
<pre><code> [self requestPingPongWithText: text
completion:^(NSString *successString, NSString *failureString)) completion {
if (successString) {
NSLog(@"successString: %@", successString); }
else if (failureString) {
NSLog(@"failureString: %@", failureString); }
}];
</code></pre>
<p>我怎样才能把我现有的代码包装成一个像上面那样的函数?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>让我们在 PingPong.h 中创建一个新的 typdef:</p>
<p><code>typedef void (^OnComplete)(std::string successString, std::string failureString);</code></p>
<p>按 block 为请求创建新方法:</p>
<pre><code>static void requestPingPongWithBlock(std:string text, OnComplete block);
</code></pre>
<p>另一种方式,使其明确:</p>
<pre><code>static void requestPingPongWithBlock(std:string text, void (^block)(std::string successString, std::string failureString));
</code></pre>
<p>在 PingPong.cpp 中的实现:</p>
<pre><code>void PingPong::requestPingPongWithBlock(std::string text, OnComplete block)
{
if (text.compare("ping") == 0)
{
block("success ping", "");
}
else
{
block("", "failure pong");
}
}
</code></pre>
<p>在 MainViewController.mm 中</p>
<pre><code>-(void)requestPingPongWithText:(NSString*)text completion:(OnComplete) compblock{
PingPong::requestPingPongWithBlock(,compblock);
}
</code></pre>
<p>你可以这样调用它:</p>
<pre><code>[self requestPingPongWithText: text
completion:^(std::string successString, std::string failureString) {
if (successString.length() != 0) {
NSLog(); }
else if (failureString.length() != 0) {
NSLog(); }
}];
</code></pre>
<p>希望对你有帮助;)</p></p>
<p style="font-size: 20px;">关于c++ - 将 C++ 函数包装成 block ,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/24494361/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/24494361/
</a>
</p>
页:
[1]