c++ - iOS 在 Objective-C 中使用 c++ 完成
<p><p><strong>示例项目 repo </strong>
<a href="https://github.com/iamZoltanVaradi/PingPong" rel="noreferrer noopener nofollow">https://github.com/iamZoltanVaradi/PingPong</a> </p>
<p>在我的应用程序中,我在 c++header 中有以下 typedef:</p>
<pre><code>typedef void (*OnComplete)(const std::string &successString, const std::string &failureString);
</code></pre>
<p>我把它放在这样的函数中。</p>
<pre><code>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);
}
</code></pre>
<p>它已在函数中被调用:</p>
<pre><code>- (void)requestPingPongWithText:(NSString*)text completion:(OnComplete) compblock{
PingPong::requestPingPongWithBlock(,compblock);
}
</code></pre>
<p>但是当我这样调用它时:</p>
<pre><code>[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");
}
}];
</code></pre>
<p>我收到以下错误:</p>
<blockquote>
<p>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 &)'</p>
</blockquote>
<p>我该如何解决这个错误?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我不确定您如何在这里使用积木。快速尝试对我来说效果不太好。但是你可以使用它(它使用我的 objc_callback 包装器):
<b>[编辑] 如果您使用 c++11 的 std::function,它确实适用于 block 。请参阅下面的代码。</b></p>
<pre><code>#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))
{
}
inline R operator ()(Ts... vs)
{
return fun_(obj_, sel_, vs...);
}
private:
SEL sel_;
id obj_;
func fun_;
};
</code></pre>
<p>希望你能从中得到想法,如果没有 - 再问一次:)</p>
<pre><code>// 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) funCallbackWithSuccess:(const std::string&)success andFail:(conststd::string&)fail
{
NSLog(@"Called with %s and %s", success.c_str(), fail.c_str());
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)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());
});
}
</code></pre>
<p>祝你好运。</p></p>
<p style="font-size: 20px;">关于c++ - iOS 在 Objective-C中使用 c++ 完成,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/24510622/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/24510622/
</a>
</p>
页:
[1]