gpt4 book ai didi

ios - Objective-C++ : Call Objective C method from C++

转载 作者:行者123 更新时间:2023-12-01 20:00:11 25 4
gpt4 key购买 nike

我正在使用 NSURLSession 连接到数据库。我已经在 Windows 的 C++ 中实现了这个,并试图让它在 iOS 上也能工作。我有一个从基 C++ 类派生的 .h 文件,它是我的 .mm 文件的 header 。如果我是正确的,我必须在 C++ 中实现我的 .h 文件中的所有函数。然而 NSURLSession 是一个 Objective-C 函数。如何从我的 C++ 函数中调用 Objective-C 方法?

我有一个名为 Connect() 的 C++ 函数,我在其中创建了一个具有 alloc 和 init 的 C++ 对象 m_Delegate。

this->m_Delegate = [[PrivateNSURLSessionDelegate alloc] initWihParent:this];
//where PrivateNSURLSessionDelegate is the name of my interface.

该接口(interface)具有我试图从中调用的 -(bool)NSConnect (在@implementation中实现):
void Connect()
{
[PrivateNSURLSessionDelegate NSConnect];
//This however gives me the error: +[PrivateNSRLSessionDelegate NSConnect]: unrecongnized selector sent to class
}

我也尝试使用我的 C++ 对象
void Connect()
{
[m_Delegate NSConnect];
//This gives me a error that is unrecognized selector sent to instance
}

有一个更好的方法吗?我基本上想要求 Objective-C 完成所有 NSURL 的工作,然后只将数据发送回 C++。

我对 Objective-C 完全陌生,因此将不胜感激任何和所有帮助!谢谢!

最佳答案

-(bool)NSConnect

这里 -表示它是一个实例方法。反过来 +将指示一个类方法。

话虽如此, [PrivateNSURLSessionDelegate NSConnect];调用一个类方法,因为你在接口(interface) PrivateNSURLSessionDelegate 上调用它.
但是,这没有定义,因为它被定义为 NSConnect被定义为实例方法(顺便说一句,约定是(实例)方法总是以小写字母开头)。
[m_Delegate NSConnect];

但是会调用实例方法。您应该定义 -(bool)NSConnectPrivateNSURLSessionDelegate的头文件中, 不高于 @implementation在实现文件中,这是一个私有(private)方法,因此无法访问。

关于ios - Objective-C++ : Call Objective C method from C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40136435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com