在 Objective C 中解析文本文件

发布于 2024-12-06 06:46:36 字数 1466 浏览 0 评论 0原文

我正在尝试解析保存在 doc 目录中的文本文件,下面显示的是它的代码

NSArray *filePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *docDirPath=[filePaths objectAtIndex:0];
NSString *filePath=[docDirPath stringByAppendingPathComponent:@"SKU.txt"];
NSError *error;
NSString *fileContents=[NSString stringWithContentsOfFile:filePath];
NSLog(@"fileContents---%@",fileContents);   
if(!fileContents)
NSLog(@"error in reading file----%@",error);
NSArray *values=[fileContents componentsSeparatedByString:@"\n"];
NSLog(@"values-----%@",values);

NSMutableArray *parsedValues=[[NSMutableArray alloc]init];
for(int i=0;i<[values count];i++){
    NSString *lineStr=[values objectAtIndex:i];
    NSLog(@"linestr---%@",lineStr);
    NSMutableDictionary *valuesDic=[[NSMutableDictionary alloc]init];
    NSArray *seperatedValues=[[NSArray alloc]init];
    seperatedValues=[lineStr componentsSeparatedByString:@","];
    NSLog(@"seperatedvalues---%@",seperatedValues);
    [valuesDic setObject:seperatedValues forKey:[seperatedValues objectAtIndex:0]];
    NSLog(@"valuesDic---%@",valuesDic);
    [parsedValues addObject:valuesDic];
    [seperatedValues release];
    [valuesDic release];
}
NSLog(@"parsedValues----%@",parsedValues);
NSMutableDictionary *result;
result=[parsedValues objectAtIndex:1];
NSLog(@"res----%@",[result objectForKey:@"WALM-FT"]);

我面临的问题是当我尝试打印 lineStr 时,即它作为单个字符串打印的文本文件的数据,所以我无法能够逐行获取内容请帮我解决这个问题。

i am trying to parse a text file saved in doc dir below show is the code for it

NSArray *filePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *docDirPath=[filePaths objectAtIndex:0];
NSString *filePath=[docDirPath stringByAppendingPathComponent:@"SKU.txt"];
NSError *error;
NSString *fileContents=[NSString stringWithContentsOfFile:filePath];
NSLog(@"fileContents---%@",fileContents);   
if(!fileContents)
NSLog(@"error in reading file----%@",error);
NSArray *values=[fileContents componentsSeparatedByString:@"\n"];
NSLog(@"values-----%@",values);

NSMutableArray *parsedValues=[[NSMutableArray alloc]init];
for(int i=0;i<[values count];i++){
    NSString *lineStr=[values objectAtIndex:i];
    NSLog(@"linestr---%@",lineStr);
    NSMutableDictionary *valuesDic=[[NSMutableDictionary alloc]init];
    NSArray *seperatedValues=[[NSArray alloc]init];
    seperatedValues=[lineStr componentsSeparatedByString:@","];
    NSLog(@"seperatedvalues---%@",seperatedValues);
    [valuesDic setObject:seperatedValues forKey:[seperatedValues objectAtIndex:0]];
    NSLog(@"valuesDic---%@",valuesDic);
    [parsedValues addObject:valuesDic];
    [seperatedValues release];
    [valuesDic release];
}
NSLog(@"parsedValues----%@",parsedValues);
NSMutableDictionary *result;
result=[parsedValues objectAtIndex:1];
NSLog(@"res----%@",[result objectForKey:@"WALM-FT"]);

The problem what i am facing is when i try to print lineStr ie the data of the text file it is printing as a single string so i could not able to get the contents in line by line way please help me solve this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淑女气质 2024-12-13 06:46:36

而是使用:

- (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator

它涵盖了几个不同的换行符。

示例:

NSArray *values = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
for (NSString *lineStr in values) {
    // Parsing code here
}

ALso seperatedValues 已过度释放。第一个是使用 alloc init 创建的,然后在下一行中它被方法 componentsSeparatedByString 替换。所以第一个od没有被释放就丢失了,那就是泄漏。后来,由 componentsSeparatedByString 创建的 seperatedValues 被释放,但它已经被 componentsSeparatedByString 自动释放,这是一个过度释放;

用ARC(自动引用计数)解决所有retain/release/autorelease问题。

这是一个使用便捷方法并省略过度发布的版本:

NSArray *values = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
for (NSString *lineStr in values) {
    NSArray *seperatedValues = [lineStr componentsSeparatedByString:@","];
    NSString *key = [seperatedValues objectAtIndex:0];
    NSDictionary *valuesDic = [NSDictionary dictionaryWithObject:seperatedValues forKey:key];
    [parsedValues addObject:valuesDic];
}
NSLog(@"parsedValues---%@",parsedValues);

Instead use:

- (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator

it covers several different newline characters.

Example:

NSArray *values = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
for (NSString *lineStr in values) {
    // Parsing code here
}

ALso seperatedValues is over released. First one is created with alloc init, then on the next line it is replaced by the method componentsSeparatedByString. So the first one od lost without being released, that is a leak. Later the seperatedValues created by componentsSeparatedByString is released but it is already auto released by componentsSeparatedByString to that is an over release;

Solve all the retain/release/autorelease problem with ARC (Automatic Reference Counting).

Here is a version that uses convenience methods and omits over release:

NSArray *values = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
for (NSString *lineStr in values) {
    NSArray *seperatedValues = [lineStr componentsSeparatedByString:@","];
    NSString *key = [seperatedValues objectAtIndex:0];
    NSDictionary *valuesDic = [NSDictionary dictionaryWithObject:seperatedValues forKey:key];
    [parsedValues addObject:valuesDic];
}
NSLog(@"parsedValues---%@",parsedValues);
扬花落满肩 2024-12-13 06:46:36

您确定文本文件中使用的行分隔符是 \n 而不是 \r (或 \r\n)?

问题可能来自于此,解释了为什么您无法将文件分成不同的行。

Are you sure the line separator used in your text file is \n and not \r (or \r\n)?

The problem may come from this, explaining why you don't manage to split the files into different lines.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文