2014-09-10 58 views
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]; 

回答

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; 

    } 
} 

似乎你也可以玩NSFileHandleoffset,但對我來說似乎更加複雜。

錯誤消息:

2014-09-10 17:37:53.888 Betterware - Database and Customer Information[15676:60b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]' 
*** First throw call stack: 
(
    0 CoreFoundation      0x017f01e4 __exceptionPreprocess + 180 
    1 libobjc.A.dylib      0x0156f8e5 objc_exception_throw + 44 
    2 CoreFoundation      0x017a48b2 -[__NSArrayI objectAtIndex:] + 210 
    3 Betterware - Database and Customer Information 0x00002746 -[ViewController retrieveinfo:] + 726 
    4 libobjc.A.dylib      0x01581880 -[NSObject performSelector:withObject:withObject:] + 77 
    5 UIKit        0x002313b9 -[UIApplication sendAction:to:from:forEvent:] + 108 
    6 UIKit        0x00231345 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61 
    7 UIKit        0x00332bd1 -[UIControl sendAction:to:forEvent:] + 66 
    8 UIKit        0x00332fc6 -[UIControl _sendActionsForEvents:withEvent:] + 577 
    9 UIKit        0x00332243 -[UIControl touchesEnded:withEvent:] + 641 
    10 UIKit        0x00270ddd -[UIWindow _sendTouchesForEvent:] + 852 
    11 UIKit        0x002719d1 -[UIWindow sendEvent:] + 1117 
    12 UIKit        0x002435f2 -[UIApplication sendEvent:] + 242 
    13 UIKit        0x0022d353 _UIApplicationHandleEventQueue + 11455 
    14 CoreFoundation      0x0177977f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15 
    15 CoreFoundation      0x0177910b __CFRunLoopDoSources0 + 235 
    16 CoreFoundation      0x017961ae __CFRunLoopRun + 910 
    17 CoreFoundation      0x017959d3 CFRunLoopRunSpecific + 467 
    18 CoreFoundation      0x017957eb CFRunLoopRunInMode + 123 
    19 GraphicsServices     0x037e45ee GSEventRunModal + 192 
    20 GraphicsServices     0x037e442b GSEventRun + 104 
    21 UIKit        0x0022ff9b UIApplicationMain + 1225 
    22 Betterware - Database and Customer Information 0x0000411d main + 141 
    23 libdyld.dylib      0x01e37701 start + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 
+0

嗨那太好了,我可以看到你是如何試圖提取的信息,但我已編輯的代碼,但現在越來越未使用的變量和數據不被重新載入?對不起,要成爲一個痛苦,但你可以看看,看看我哪裏出錯了? (我編輯了原帖) – user3788098 2014-09-10 14:00:38

+0

@ user3788098:我編輯了我的答案。 – Larme 2014-09-10 14:10:34

+0

輝煌的它現在的作品! Theres只有一個問題,當[allLines firstObject];它工作並加載數據庫中的第一個。你是正確的,但我希望在數據庫中有最後一個,但是當它的[allLines lastObject];我收到錯誤消息@autoreleasepool { return UIApplicationMain(argc,argv,nil,NSStringFromClass([AppDelegate class])); } – user3788098 2014-09-10 15:33:33