【问题标题】:How to deal with this NSString correctly with cocos2d?如何用 cocos2d 正确处理这个 NSString?
【发布时间】:2013-01-02 23:15:10
【问题描述】:

我是 cocos2d 的新手,所以我的应用内购买类助手有问题。我在 Cocoa Touch 中编写了游戏,这个类运行良好,但我现在在 Cocos2d 中编写相同的游戏,问题出在 NSString 上。

这里有一些sn-ps。

当我单击某个按钮时,首先调用此方法。如您所见,completeIdentifier 是带有bundleIdentifier 和一些字符串参数的简单字符串。没关系,在这个地方我可以记录这个completeIdentifier

- (void)prepareToPurchaseItemWithIdentifier:(NSString *)aIdentifier showAlertWithTitle:(NSString *)title description:(NSString *)description delegate:(id)aDelegate{

    self.delegate = aDelegate;

    identifier = aIdentifier;
    completeIdentifier = [NSString stringWithFormat:@"%@.%@", [[NSBundle mainBundle] bundleIdentifier], aIdentifier];

    askToPurchase = [[UIAlertView alloc] initWithTitle:title message:description delegate:self cancelButtonTitle:nil otherButtonTitles:@"Later", @"Yes", nil];
    askToPurchase.delegate = self;
    [askToPurchase show];
}

下一个方法是UIAlertViewDelegate 方法。当我从第一种方法单击alertView 上的YES 时调用。

#pragma mark - UIAlertViewDelegate Methods
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSLog(@"%@", completeIdentifier);
    if (alertView == askToPurchase) {

        NSLog(@"[TSIAPHelper] Clicked YES. Prepare...");
        if ([SKPaymentQueue canMakePayments]) {

            NSLog(@"[TSIAPHelper] Prepare to purchase [%@].",completeIdentifier);
            SKProductsRequest *request =[[SKProductsRequest alloc] initWithProductIdentifiers:
                                     [NSSet setWithObject:completeIdentifier]];
            request.delegate = self;
            [request start];

            pleaseWait = [[UIAlertView alloc] initWithTitle:@"Please wait..." message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
            UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
            [activity startAnimating];
            [pleaseWait addSubview:activity];
            activity.frame = CGRectMake(125, 50, 36, 36);
            [pleaseWait show];
        }
        else {

            NSLog(@"[TSIAPHelper] Purchase [FAILURE]. Prohibited by Parentar Control or something like that.");
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Prohibited." message:@"Parental Control is enabled, cannot make a purchase. Turn off and try again." delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
            [alert show];
        }
    }
}

问题是:无论何时何地,我都想记录变量completeIdentifier,我选择的行崩溃了:0x39e965d0: ldr r3, [r4, #8],但我不知道那是什么意思。并选择了这一行:

NSLog(@"%@", completeIdentifier);

我该如何解决?在 Cocoa Touch 中运行良好。当我使用 cocos2d 时不是。

【问题讨论】:

  • 当我删除所有completeIdentifier 或其他存储此变量的变量时:[NSString stringWithFormat:@"%@.%@", [[NSBundle mainBundle] bundleIdentifier], aIdentifier] 并将[NSString stringWithFormat:@"%@.%@", [[NSBundle mainBundle] bundleIdentifier], aIdentifier] 直接粘贴到 NSLog 或其他地方,它工作正常。这对我来说很奇怪......谁知道问题出在哪里?

标签: objective-c crash nsstring cocos2d-iphone nslog


【解决方案1】:

我猜你没有使用 ARC。在这种情况下,completeIdentifier 将被自动释放。

Cocos2d 每帧都会清空自动释放池,而在 Cocoa 中这并没有严格定义,但仍然可能会崩溃。您可以通过保留或复制字符串来解决此问题。

completeIdentifier = [NSString stringWithFormat:@"%@.%@", [[NSBundle mainBundle] bundleIdentifier], aIdentifier];
completeIdenfitier = [completeIdentifier retain];

【讨论】:

  • 谢谢!这是个问题。
猜你喜欢
  • 2012-02-27
  • 1970-01-01
  • 2012-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多