从字符串检索特定文本行的内存管理问题 [英] Memory management issues with retrieving particular lines of text from string

查看:43
本文介绍了从字符串检索特定文本行的内存管理问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS开发的新手.

I am kind of new to iOS development.

我想从我拥有的text(.rtf)文件中检索字符串.该文件在我的应用程序主捆绑包内.它的内容是:

I want to retrieve strings from the text(.rtf) file I have. The file is within my application main bundle. It's content are:

#start word1 First word end word2 Second word end //lots of things to be added later

代码:

 path = [[NSBundle mainBundle]pathForResource:@"words" ofType:@"rtf"];
    if(path)
    {
        NSLog(@"path exists");
    }
    NSError *error = nil;

    NSString *file = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
    if(error)
    {
        NSLog(@"error");
    }
    NSString *finalword= [[NSString alloc]init ];
    NSString *startfrom = [[NSString alloc] initWithFormat:@"word%i",i+1];
    i++;
    NSLog(@"%@",startfrom);
    NSString *wordoftheday = [[NSString alloc]init ];
    NSScanner *scanner = [NSScanner scannerWithString:file];
    [scanner scanUpToString:startfrom intoString:nil];
    [scanner scanUpToString:@"end" intoString:&wordoftheday];
    finalword = [wordoftheday substringFromIndex:[startfrom length]];
    NSLog(@"%@",finalword);
    Word.text = final word;  //change label text
//[final word release];
//[wordoftheday release];
//[file release];

代码可以正常工作,但是它给我留下了内存管理问题.如果我在上一个注释的代码中释放变量,则应用程序崩溃.

Code is working fine but it leaves me with memory management issues. App crashes if I release the variables in the last commented code.

这个方法也在我的viewdidload中.我希望标签在用户单击按钮时更改文本.我将不得不在该方法中再次编写相同的代码,这给我带来了更多的内存问题.

Also this method is in my viewdidload. I want the label to change text when user click a button. I will have to write the same code again in that method which leaves me with more memory issue.

推荐答案

所以一一看待这些,着重于内存问题,而不是这里的整体策略:

So looking at these one by one, focusing on the memory issues and not the overall strategy here:

NSString * finalword = [[NSString alloc] init];

在这里,您分配/初始化一个新的不可变且为空的NSString,然后无论如何您最终都会覆盖指向该指针的指针.您应该只删除此行.然后,您需要将声明向下移动几行:

Here you alloc/init a new immutable and empty NSString, and then you end up overwriting the pointer to this later anyway. You should just delete this line. And then you'll need to move the declaration down a few lines to this:

NSString * finalword = [wordoftheday substringFromIndex:[从长度开始]];

那么你有:

NSString * startfrom = [[NSString alloc] initWithFormat:@"word%i",i + 1];

您需要稍后发布的版本.或者只是将其更改为:

This one you need to release later. Or just change it to:

NSString * startfrom = [NSString stringWithFormat:@"word%i",i + 1];

然后您拥有:

NSString * wordoftheday = [[NSString alloc] init];

与最终词相同的故事.除非您确实需要定义此变量,以便稍后将其传递给扫描仪.因此将其更改为:

Same story as finalword. Except that you do need to define this variable so you can pass it to the scanner later. So change it to:

NSString * wordoftheday = nil;

最后,您可以释放文件".那样就好.但是您不希望发布"wordoftheday"或"finalword",因为您不拥有这些字符串.您不是自己创建的.

And lastly, you can release 'file'. That is fine. But you don't want to release 'wordoftheday' or 'finalword' because you don't own those strings. You did not create them yourself.

和另一注:

如果(错误)

这不是检查加载文件"时错误的正确方法.您应该检查方法的返回值,然后仅当返回值为nil时,才查找错误.因此,将该行更改为:

That is not the correct way to check for an error in loading 'file'. You should check the return from the method and then look for an error if and only if the return value was nil. So change that line to:

if(!file)

(好的,这并不是真正的内存问题,但是我确实注意到了一个错误.)

(OK, that wasn't really a memory issue, but a bug I did notice.)

我认为,至少就内存问题而言,这就是全部.希望对您有所帮助.

I think that's all of it at least as far as memory issues. I hope that helps.

这篇关于从字符串检索特定文本行的内存管理问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆