我正在从 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/