在 Objective C 中解析文本文件
我正在尝试解析保存在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
而是使用:
它涵盖了几个不同的换行符。
示例:
ALso
seperatedValues
已过度释放。第一个是使用 alloc init 创建的,然后在下一行中它被方法componentsSeparatedByString
替换。所以第一个od没有被释放就丢失了,那就是泄漏。后来,由componentsSeparatedByString
创建的seperatedValues
被释放,但它已经被componentsSeparatedByString
自动释放,这是一个过度释放;用ARC(自动引用计数)解决所有retain/release/autorelease问题。
这是一个使用便捷方法并省略过度发布的版本:
Instead use:
it covers several different newline characters.
Example:
ALso
seperatedValues
is over released. First one is created with alloc init, then on the next line it is replaced by the methodcomponentsSeparatedByString
. So the first one od lost without being released, that is a leak. Later theseperatedValues
created bycomponentsSeparatedByString
is released but it is already auto released bycomponentsSeparatedByString
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:
您确定文本文件中使用的行分隔符是
\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.