如何编写 C++ Objective C 的处理类
// ------------------------------------------------ --------------------
更新:我搞砸了。我在 AppDelegate->onApplicationWillResignActive 中直接留下了对 openfeint 的调用,这导致了编译器 C++ 错误。
我的歉意,如果有人想尝试同样的事情,单身人士确实可以工作。请确保在 .m 文件中包含标头而不是头文件。
// ------------------------------------------------ --------------------
我正在构建一个 iPhone 应用程序,并使用用 C++ 编写的 Openfeint SDK/Library/Framework (??)。
我想知道是否可以编写一个与 C++ 交互的类,这样我就不必将 ObjC 类更改为 .mm 文件。
我尝试创建一个单例,希望可以将其标头包含在正常的 .m 文件中,但这不起作用,我仍然需要制作包含标头 .mm 的文件
我想这样做的原因(或类似的东西)是因为我没有 C++ 经验,并且将 ObjC 更改为 C++ 文件会导致错误和警告。
这是我创建的单例......
// --------------------------------------------------------------------
// OpenfeintController.h
// --------------------------------------------------------------------
#import <Foundation/Foundation.h>
@interface OpenfeintController : NSObject {
NSString *productKey, *secretKey, *displayName;
}
+(OpenfeintController*)sharedOpenfeintController;
- (void) initializeWithProductKey:(NSString *)pKey andSecretKey:(NSString *)sKey andDisplayName:dName;
- (void) launchOpenFeint;
- (void) submitHighScoreToLeaderboard:(NSString *)leaderboardId;
@end
实现
// --------------------------------------------------------------------
// OpenfeintController.mm
// --------------------------------------------------------------------
#import "OpenfeintController.h"
#import "OpenFeint.h"
static OpenfeintController *singletonOpenfeintController = nil;
@implementation OpenfeintController
+(OpenfeintController*)sharedOpenfeintController {
@synchronized(self) {
if (!singletonOpenfeintController) {
singletonOpenfeintController = [[OpenfeintController alloc] init];
}
}
return singletonOpenfeintController;
}
- (void) initializeWithProductKey:(NSString *)pKey andSecretKey:(NSString *)sKey andDisplayName:dName
{
//[OpenFeint initializeWithProductKey:pKey andSecret:sKey andDisplayName:dName andSettings:nil andDelegates:nil];
}
- (void) launchOpenFeint
{
}
- (void) submitHighScoreToLeaderboard:(NSString *)leaderboardId
{
}
@end
// --------------------------------------------------------------------
UPDATE: I messed up. I left a call directly to openfeint in my AppDelegate->onApplicationWillResignActive which was causing the compiler C++ error.
My Appologies, the singleton does work should anyone be thinking of trying the same thing. Just be sure to include the header in the .m file and not the header file.
// --------------------------------------------------------------------
I'm building an iPhone app, and using the Openfeint SDK/Library/Framework (??) which is written in C++.
I'm wondering, is it possible to write a class to interface with C++ so that I don't have to change my ObjC classes to .mm files.
I've tried creating a singleton, in the hope that I could include it's header in normal .m files, but that doesn't work, I still need to make the file that includes the header .mm
The reason I want to do this (or something like this) is because I've no experience with C++, and changing ObjC to C++ files has been causing errors and warnings.
Here's the singleton I created...
// --------------------------------------------------------------------
// OpenfeintController.h
// --------------------------------------------------------------------
#import <Foundation/Foundation.h>
@interface OpenfeintController : NSObject {
NSString *productKey, *secretKey, *displayName;
}
+(OpenfeintController*)sharedOpenfeintController;
- (void) initializeWithProductKey:(NSString *)pKey andSecretKey:(NSString *)sKey andDisplayName:dName;
- (void) launchOpenFeint;
- (void) submitHighScoreToLeaderboard:(NSString *)leaderboardId;
@end
Implmentation
// --------------------------------------------------------------------
// OpenfeintController.mm
// --------------------------------------------------------------------
#import "OpenfeintController.h"
#import "OpenFeint.h"
static OpenfeintController *singletonOpenfeintController = nil;
@implementation OpenfeintController
+(OpenfeintController*)sharedOpenfeintController {
@synchronized(self) {
if (!singletonOpenfeintController) {
singletonOpenfeintController = [[OpenfeintController alloc] init];
}
}
return singletonOpenfeintController;
}
- (void) initializeWithProductKey:(NSString *)pKey andSecretKey:(NSString *)sKey andDisplayName:dName
{
//[OpenFeint initializeWithProductKey:pKey andSecret:sKey andDisplayName:dName andSettings:nil andDelegates:nil];
}
- (void) launchOpenFeint
{
}
- (void) submitHighScoreToLeaderboard:(NSString *)leaderboardId
{
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这当然是可能的,遗憾的是您没有真正提供任何可以帮助我们确定您遇到的问题的信息。您说
OpenfeintController.h
标头的用户需要是 Objective-C++,但您发布的标头中似乎没有包含任何 C++(因此这不是必需的) )。如果执行此操作时编译出现错误,请发布错误,以便我们可以看到实际发生的情况。
This is certainly possible to do, unfortunately you’ve not really given any information that could help us determine what problems you are having. You say that users of the
OpenfeintController.h
header need to be Objective-C++, but there doesn’t appear to be any C++ included in the header you’ve posted (so this shouldn’t be necessary).If the compilation is giving errors when you do this, then post the errors so we can see what is actually happening.
我用不同的 C++ 库 (GeographicLib) 做了类似的事情,它工作没有问题。只需确保 .h 文件中没有 C++ 代码或导入的 C++ 标头即可。不过,我没有发现您的代码有任何问题,所以恐怕我无法真正帮助您。我没有使用 OpenFeint 的经验。
I do something similar with a different C++ library (GeographicLib) and it works without problems. Just make sure that no C++ code or imported C++ headers are in your .h file. I don't see anything wrong with your code, though, so I'm afraid I can't really help you. I have no experience with OpenFeint.