【发布时间】:2011-09-23 10:32:36
【问题描述】:
我正在尝试解析保存在 doc dir 中的文本文件,下面显示的是它的代码
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 时,即它作为单个字符串打印的文本文件的数据,所以我无法逐行获取内容请帮我解决这个问题.
【问题讨论】:
-
请注意,
stringWithContentsOfFile:已弃用。它使您无法控制编码。 developer.apple.com/library/mac/documentation/Cocoa/Reference/… -
如果您正在解析 CSV 文件,您可能需要考虑使用 already been written to do that 的代码。
标签: iphone objective-c ios text-parsing