English 中文(简体)
失去了我的绳子
原标题:losing my string

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

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字符串WithFormat传入字符串时,它可能会工作5-8次,然后以某种方式出错,并将完全随机的东西发送给函数,如-36.657。

这怎么可能?参数中没有任何变化,因为_director.platformPrefix是在程序开始时设置的,从未更改。唯一改变的是creditPos用字符串进行选择以创建+传递给函数。不知怎的,在一些迭代之后,正在创建的字符串只是胡言乱语,试图将其与传入的最后一个字符串进行比较会导致代码崩溃,而不会返回任何错误。

帮助:(

最佳回答

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

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

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

问题回答

听起来好像其中一根弦被解开了。可能是过度释放,或者对内存警告或viewDidUnload没有正确反应。

出于调试目的,请尝试保留_director.platformPrefix并打印出该对象的retainCount。如果问题消失了,你就真的遇到了零售价的问题。在几乎99%的情况下,最终的解决方案不仅仅是保留它,而是找到错误发布它的代码。

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

在页眉中创建:

@property (nonatomic, retain) NSString *imageName;

以及在您的实施过程中:

@synthesize imageName;

或者,添加[inImageName retain];当您将其指定给imageName时。(只需确保在分配imageName字符串之前以及在dealloc方法中释放该字符串即可。)

我同意奥尔特温的观点;听起来platformPrefix没有被正确地保留,在被覆盖之前,它设法保持了一段时间的完整性。





相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签