显示上次提交的.csv文件数据?

时间:2014-09-10 10:12:21

标签: objective-c 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];
    self.customername.text=@"";
    self.houseno.text=@"";
    self.customerpostcode.text=@"";
    self.catno.text=@"";
    self.date.text=@"";
    _orderval.value=0;
    _reorderval.value=0;
    _ordervalresult.text=@"£0.00";
    _reordervalresult.text=@"£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];

1 个答案:

答案 0 :(得分:1)

我这样做:

-(IBAction)pushRetrieveLastInfo:(id)sender
{
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
    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];
            //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

        self.resultview.text=lastEntry;

    }
}

或者:

-(IBAction)pushRetrieveLastInfo:(id)sender
{
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
    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];
            //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];
        NSString *lastCatNo = [lastData objectAtIndex:3];
        NSString *lastOrderValResult = [lastData objectAtIndex:4];
        NSString *lastReorderValResult = [lastData objectAtIndex:5];
        NSString *lastDate = [lastData objectAtIndex:6];

        NSString *textResult = [NSString stringWithFormat:@"Name: %@\nHouse Nb: %@\n, Last Postcode: %@\n, Last Cat Nb: %@\n, Last Order: %@\n, Last Reorder: %@\n, Last date: %@", lastCustomerName, lastHouseNo, lastCustomerPostcode, lastCatNo, lastOrderValResult, lastReorderValResult, lastDate];
        self.resultview.text=textResult;

    }
}

似乎您也可以使用offset的{​​{1}},但对我来说似乎更复杂。

错误讯息:

NSFileHandle