我需要读取的close.txt文档中格式如下:
用下面蓝色标记的三行代码即可读取每行数据连成的字符串,然后再对每行数据进行处理,然后封装到数据模型MACDModel中去。
self.arr = [NSMutableArray arrayWithCapacity:0];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"close" ofType:@"txt"];
NSString *dataFile = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSArray *dataarr = [dataFile componentsSeparatedByString:@"\n"];
for (NSString *str in dataarr) {
NSArray *temp = [str componentsSeparatedByString:@"\t"];
NSString *dataTime = temp[0];
CGFloat closePrice = [temp[1] floatValue];
CGFloat highPrice = [temp[2] floatValue];
NSString *str2 = [temp[3] stringByReplacingOccurrencesOfString:@"\r" withString:@""];
CGFloat lowPrice = [str2 floatValue];
MACDModel *macdModel = [[MACDModel alloc] initWithDataTime:dataTime closePrice:closePrice highPrice:highPrice lowPrice:lowPrice];
[self.arr addObject:macdModel];
}