【发布时间】:2011-12-13 05:39:52
【问题描述】:
我对 iOS 开发有点陌生。
我想从我拥有的 text(.rtf) 文件中检索字符串。该文件位于我的应用程序主包中。它的内容是:
#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];
代码运行良好,但它给我留下了内存管理问题。如果我释放最后注释代码中的变量,应用程序会崩溃。
这个方法也在我的viewdidload中。我希望标签在用户单击按钮时更改文本。我将不得不以这种方法再次编写相同的代码,这给我留下了更多的内存问题。
【问题讨论】:
标签: iphone objective-c ios xcode