0
我对这个问题有点搜索,但似乎找不到解决方案,所以希望你能帮助。显示上次提交的.csv文件的数据?
基本上我已经构建了一个应用程序,可以将客户输入数据保存到.csv文件。现在我希望在应用程序中显示这些信息,但只显示最后输入的用户数据(用户的其他数据需要保存到.csv中,但不需要在数据库稍后才能查看)我可以加载它所有的数据,但我只想要最后提交的数据显示在应用程序中...这是我的代码到目前为止;
-(IBAction)saveinfo:(id)sender{
NSString *resultLine=[NSString stringWithFormat:@"%@ , %@ , %@ , %@ , %@ , %@, %@\n\n\n\n\n\n\n",
self.customername.text,
self.houseno.text,
self.customerpostcode.text,
self.catno.text,
_ordervalresult.text,
_reordervalresult.text,
self.date.text
];
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
//resultview.text = docPath;
NSString *surveys=[docPath stringByAppendingPathComponent:@"customerdata.csv"];
if (![[NSFileManager defaultManager] fileExistsAtPath:surveys]) {
[[NSFileManager defaultManager]
createFileAtPath:surveys contents:nil attributes:nil];
}
NSFileHandle *filehandle = [NSFileHandle fileHandleForUpdatingAtPath:surveys];
[filehandle seekToEndOfFile];
[filehandle writeData:[resultLine dataUsingEncoding:NSUTF8StringEncoding]];
[filehandle closeFile];
[email protected]"";
[email protected]"";
[email protected]"";
[email protected]"";
[email protected]"";
_orderval.value=0;
_reorderval.value=0;
_ordervalresult.tex[email protected]"£0.00";
[email protected]"£0.00";
NSLog(@"surveys path: %@", surveys);
}
-(IBAction)retrieveinfo:(id)sender {
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
//resultView.text = docPath;
NSString *surveys=[docPath stringByAppendingPathComponent:@"customerdata.csv"];
if([[NSFileManager defaultManager] fileExistsAtPath: surveys])
{
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:surveys];
NSString *surveyResults =[[NSString alloc]initWithData:[fileHandle availableData] encoding:NSUTF8StringEncoding];
[fileHandle closeFile];
self.resultview.text=surveyResults;
}
}
任何人都知道如何解决这个问题?我已经尝试了一些想法,但可以只加载所有的信息,或者根本就没有...
谢谢您的时间
编辑:更新的代码
-(IBAction)retrieveinfo:(id)sender {
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
_resultview.text = docPath;
NSString *surveys=[docPath stringByAppendingPathComponent:@"customerdata.csv"];
if([[NSFileManager defaultManager] fileExistsAtPath: surveys])
self.resultview.text=surveys;
//This is a part of your code from retrieveinfo:sender:
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:surveys];
NSString *surveyResults =[[NSString alloc]initWithData:[fileHandle availableData] encoding:NSUTF8StringEncoding];
[fileHandle closeFile];
//All lines of you CSV file
NSArray *allLines = [surveyResults componentsSeparatedByString:@"\n"];
//To get Last Line
NSString *lastEntry = [allLines lastObject]; //or [allLines firstObject]; depending on the order your build it, but if I understood correctly it should be lastObject
NSArray *lastData = [lastEntry componentsSeparatedByString:@","];
NSString *lastCustomerName = [lastData objectAtIndex:0];
NSString *lastHouseNo = [lastData objectAtIndex:1];
NSString *lastCustomerPostcode = [lastData objectAtIndex:2];
嗨那太好了,我可以看到你是如何试图提取的信息,但我已编辑的代码,但现在越来越未使用的变量和数据不被重新载入?对不起,要成为一个痛苦,但你可以看看,看看我哪里出错了? (我编辑了原帖) – user3788098 2014-09-10 14:00:38
@ user3788098:我编辑了我的答案。 – Larme 2014-09-10 14:10:34
辉煌的它现在的作品! Theres只有一个问题,当[allLines firstObject];它工作并加载数据库中的第一个。你是正确的,但我希望在数据库中有最后一个,但是当它的[allLines lastObject];我收到错误消息@autoreleasepool { return UIApplicationMain(argc,argv,nil,NSStringFromClass([AppDelegate class])); } – user3788098 2014-09-10 15:33:33