objective-c - 如何用cocos2d正确处理此NSString?

标签 objective-c crash nsstring cocos2d-iphone nslog

我是cocos2d的新手,因此我的应用程序内购买类帮助器存在问题。我用Cocoa Touch编写了游戏,并且该类完美地工作了,但是现在我正在Cocos2d中编写相同的游戏,问题是NSString。

这是一些片段。

当我单击某个按钮时,此方法称为第一个。如您所见,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方法。当我从第一种方法单击YES上的alertView时被调用。
#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不是。

最佳答案

我想您不是在使用ARC。在这种情况下,completeIdentifier将被自动释放。

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

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

关于objective-c - 如何用cocos2d正确处理此NSString?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14130751/

相关文章:

iphone - Nsstring对象更改其数据类型

objective-c - 将对象设置为 nil 时不调用 dealloc 方法

ios - 为什么这不会崩溃?

swift - 如何使用 Swift 将每个句子中的第一个单词大写

iOS 调度异步崩溃

java - 为什么 'toast' 消息在 Android 中不起作用?无法修复bug,请帮忙!

ios - 如何将 UTF8String 转换为基于 JSON 的 NSDictionary?

ios - 为什么我的委托(delegate)方法从未被调用过?

objective-c - 标签栏 Controller 项目从右侧开始

ios - subview Controller 在呈现和关闭模态视图后失去委托(delegate)