我正在从C ++实例创建Objective C类的实例。问题是,当试图获取某些变量的值时(在obj c实例中)我总是得到0.有些NSLog也被忽略了!:
目标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;
}
testApp.h class testApp:public ofxiPhoneApp { 上市: void goToStoreFromMenu(); void checkPurchase(); InAppPurchaseManager * theStore; }
testApp.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";
}
结果总是完成? 0 ,即使我在购买中将其设置为1。此外 NSLog(@“STORE:HELLO THERE”); 被忽略
我不明白发生了什么
答案 0 :(得分:1)
在goToStoreFromMenu
中,声明一个名为theStore
的新局部变量。在checkPurchase
中,您引用了一些具有相同名称的其他变量。 goToStoreFromMenu
函数初始化局部变量,该变量在函数末尾超出范围。您需要初始化最终在checkPurchase
中引用的相同变量。