c++ - 从 C++ 类调用变量到 Objective C 类

标签 c++ objective-c objective-c++ openframeworks

我正在从 C++ 实例创建 Objective C 类的实例。问题是当试图获取某些变量的值时(在 obj c 实例中)我总是得到 0。一些 NSLogs 也被忽略了!:

Objective-C 类: InAppPurchaseManager.h

@interface InAppPurchaseManager : NSObject <SKProductsRequestDelegate, SKPaymentTransactionObserver>{
    SKProduct *proUpgradeProduct;
    SKProductsRequest *productsRequest;
@public
    int finishedPurchaseProcess;
}
- (int)hasFinishedPurchaseProcess;
- (void)purchase;
@end

InAppPurchaseManager.m

@implementation InAppPurchaseManager
- (void)purchase{
    finishedPurchaseProcess=1;
}
- (int)hasFinishedPurchaseProcess{
    NSLog(@"STORE: HELLO THERE");   
    return finishedPurchaseProcess;
}

测试应用.h class testApp : public ofxiPhoneApp { 民众: void goToStoreFromMenu(); void checkPurchase(); InAppPurchaseManager * theStore;

测试应用.mm

// First I call ghis function
void testApp::goToStoreFromMenu(){  
    InAppPurchaseManager* theStore = [[InAppPurchaseManager alloc] init];
    [theStore purchase];
}

// And then this function
void testApp::checkPurchase(){  
    cout << "Finished? " << [theStore hasFinishedPurchaseProcess] << "\n";
}

结果总是Finished? 0,即使我在购买中设置为1。 NSLog(@"STORE: HELLO THERE"); 也被忽略

我不明白这是怎么回事

最佳答案

goToStoreFromMenu 中,您声明了一个名为 theStore 的新局部变量。在 checkPurchase 中,您引用了一些具有相同名称的 other 变量。 goToStoreFromMenu 函数初始化局部变量,该变量在函数结束时超出范围。您需要初始化最终将在 checkPurchase 中引用的同一个变量。

关于c++ - 从 C++ 类调用变量到 Objective C 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5291983/

相关文章:

c++ - 为什么在定义结构的优先级队列时使用 vector ?

iphone - 对于 Iphone,如何从当前加载的 View 引用 Root View Controller ,以便可以替换/切换 View

iphone - 关闭 ViewController : not working

objective-c - 尺寸已定义

c++ - 我如何发送(和接收)非常大的 float 数组从 Swift 到 Objective c++ 再到 c++,然后备份到 Swift?

c++ - 为什么此代码使用 openMP 提供 SIGABRT?

java - OpenGL 正确附加纹理

c++ - Const Rvalue 引用以捕获不应编译的重载

c++ - 在 C++ 中为 iOS 开发(有/没有 Xcode)

objective-c - 如何拥有不同的 NSManagedObjectContext?