失去我的弦

时间:2010-10-13 15:52:16

标签: iphone objective-c xcode

我遇到了字符串创建和比较的问题,似乎丢失了它的内容。目前我有这个:

switch (creditPos) 
{
    case 0:
        [creditCart.faceImage setImage:[NSString stringWithFormat:@"%@credits_face1.png", _director.platformPrefix]];
        break;
    case 1:
        [creditCart.faceImage setImage:[NSString stringWithFormat:@"%@credits_face2.png", _director.platformPrefix]];
        break;
    case 2:
        [creditCart.faceImage setImage:[NSString stringWithFormat:@"%@credits_face3.png", _director.platformPrefix]];
        break;
    case 3:
        [creditCart.faceImage setImage:[NSString stringWithFormat:@"%@credits_face4.png", _director.platformPrefix]];
        break;
    case 4:
        [creditCart.faceImage setImage:[NSString stringWithFormat:@"%@credits_face5.png", _director.platformPrefix]];
        break;
    default:
        break;
}

faceImage是我创建的一个对象,在setImage的函数里面我有...

- (void)setImage:(NSString *)inImageName {

NSLog(@"Before Break");

// By default set the scale to 1.0f and the filtering to GL_NEAREST
if(![imageName isEqualToString:inImageName])
{
    NSLog(@"Hit");}

我遇到的问题是,当我使用NSString stringWithFormat传入字符串时,它可能会工作5-8次,然后以某种方式进行窃听并将一些完全随机的内容发送到-36.657等函数。

这怎么可能?参数中没有任何内容正在改变,因为_director.platformPrefix是在程序开头设置的,并且从未改变过。唯一改变的是creditPos,用string来创建+传递给函数。不知何故,在一些迭代之后创建的字符串只是乱码,并试图将它与传递的最后一个字符串进行比较,而不会丢失任何错误。

帮助:(

4 个答案:

答案 0 :(得分:0)

听起来其中一个字符串被释放。可能是过度释放或未正确响应内存警告或viewDidUnload。

出于调试目的,请尝试保留_director.platformPrefix并打印出该对象的retainCount。如果问题消失了,你确实是一个retainCount问题。在近99%的案例中,最终的解决方案不仅仅是保留它,而是找到错误地释放它的代码。

答案 1 :(得分:0)

如何将inImageName分配给imageName。如果您没有使用属性而您没有保留,那么您的inImageName字符串将被垃圾收集。 stringWithFormat方法将返回一个自动释放的字符串,除非你保留它,否则它将被自动清除。

在标题中创建:

@property (nonatomic, retain) NSString *imageName;

并在您的实施中:

@synthesize imageName;

或者,添加[inImageName retain];在某处,当你将它分配给imageName。 (只需确保在分配之前和dealloc方法中释放imageName字符串。)

答案 2 :(得分:0)

我同意奥特温;听起来像platformPrefix没有被正确保留,并且它在被覆盖之前设法保持原样。

答案 3 :(得分:-1)

你可能想尝试分配字符串,因为这可能是一个自动释放问题。

NSString *myString = [[NSString alloc] initWithFormat:@"%@credits_face%d.png",_director.platformPrefix, creditPos+1];
[creditCart.faceImage setImage:myString];

然后让setImage方法释放字符串。

相关问题