gpt4 book ai didi

ios - 读取 csv 文件 ios

转载 作者:行者123 更新时间:2023-11-29 10:48:52 25 4
gpt4 key购买 nike

我在读取 csv 文件时遇到问题。仅显示 csv 文件的最后一行。但是在我的 fetchedResultsController 中我有 2 行这是代码:

NSString * writeString;
NSInteger i = 0;
for (id object in [[self fetchedResultsController] fetchedObjects]) {
NSString * object1 = [[object valueForKey:@"object1"] description];
NSString * object2 = [[object valueForKey:@"object2"] description];
NSString * object3 = [[object valueForKey:@"object3"] description];
NSString * object4 = [[object valueForKey:@"object4"] description];
writeString = [NSString stringWithFormat:@"%@, %@, %@, %@, \n", object1, object2, object3, object4];
i++;
NSLog(@"%@ - %i", writeString, i);

}


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* savePath = [paths objectAtIndex:0];
savePath = [savePath stringByAppendingPathComponent:@"myfile.csv"];
[writeString writeToFile:savePath atomically:YES encoding:NSUTF8StringEncoding error:NULL];
NSLog(@"%@", savePath);

NSString *fullPath = savePath;

[self readTitleFromCSV:fullPath AtColumn:0];

还有 readTitleFromCSV 方法:

-(void)readTitleFromCSV:(NSString*)path AtColumn:(int)column
{

NSMutableArray *titleArray=[[NSMutableArray alloc]init];

NSString *fileDataString=[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSArray *linesArray=[fileDataString componentsSeparatedByString:@"\n"];


int k=0;
for (id string in linesArray)
if(k<[linesArray count]-1){

NSString *lineString=[linesArray objectAtIndex:k];
NSArray *columnArray=[lineString componentsSeparatedByString:@","];
[titleArray addObject:[columnArray objectAtIndex:column]];
k++;

}

NSLog(@"%@",titleArray);

}

感谢您的帮助。

最佳答案

尝试使用 NSMutableString 而不是 NSString:

NSMutableString *writeString = [NSMutableString string];

然后在for循环中:

[writeString appendString:[NSString stringWithFormat:@"%@, %@, %@, %@, \n", object1, object2, object3, object4]];

关于ios - 读取 csv 文件 ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21307953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com