2013-09-29 103 views
0

我一直在寫一個簡單的應用程序,從.csv文件中繪製初始值,然後添加到它們,然後嘗試將修改的數據寫回到同一個CSV文件。終端窗口中的輸出表明它的工作正常,但是當我查看該文件或從另一啓動(我在Xcode的iPhone模擬器上測試這個文件)中訪問它時,它似乎不起作用。有人能告訴我我要去哪裏嗎?這裏是我的代碼:字符串似乎不會覆蓋.csv文件。爲什麼?

-(IBAction)AddButtonPressed:(id)sender{ 

// Get the input from the input field and add it to the sum in the bank field 
float a = ([input.text floatValue]); 
float b = a+([earned.text floatValue]); 

// adding the old value of 'earned' text field to the value from the input field and update the interface 
earned.text = [[NSString alloc] initWithFormat:@"%4.2f",b]; 

// THIS IS THE BEGINNINGS OF THE CODE FOR WRITING OUT TO A .CSV FILE SO THAT DATA CAN BE USED LATER IN EXCEL 
NSString *path = [[NSBundle mainBundle] pathForResource:@"Income" ofType:@"csv"]; 
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) 
{ 
    NSLog(@"found it"); 
    NSString *contents = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; 
    NSLog(@"string looks like this\: %@", contents); 

    //set the contents of income to be the same as what's currently in income.csv 
    [income setString:contents]; 
    NSLog(@"income contains\: %@", income); 



    //NEXT Apend income to add NSDATE, Textinput and the float value 'a' 

    //Get the Date... 
    NSDateComponents *components = [[NSCalendar currentCalendar] components: NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]]; 
    NSInteger day = [components day]; 
    NSInteger month = [components month]; 
    NSInteger year = [components year]; 

    //... And apend it into a readable string with a comma on the end 
    NSString *theDate = [[NSString alloc] init]; 
    theDate = [NSString stringWithFormat:@"%d.%d.%d", day,month,year]; 
    NSLog(@"The Date is %@", theDate); 


    //holder string till I get the text input wired up in the interface 
    NSString *text = [[NSString alloc]init]; 
    text = @"filler words"; 

    //turn the float entered in the GUI to a string ready for adding to the 'income' mutable array 
    NSString *amountAdded = [[NSString alloc]init]; 
    amountAdded = [NSString stringWithFormat:@"%4.2f", a]; 

    //format the final string and pass it to our 'income' NSMutable Array 
    NSString *finalString = [[NSString alloc] init]; 
    finalString = [NSString stringWithFormat:@"\n %@, %@, %@", theDate, text, amountAdded]; 
    NSLog(@"final string is %@", finalString); 
    [income appendString:finalString]; 
    NSLog(@"income now reads %@", income); 

    [income writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil]; 



    NSLog(@"completed writing to income.csv which now reads %@", contents); 

} 
else{ 
    NSLog(@"not a sausage"); 
} 
+1

什麼是「收入「我不明白你在哪裏你聲明/初始化那個對象,你也可以嘗試NSError *錯誤; [收入writeToFile:原子路徑:YES編碼:NSUTF8StringEncoding錯誤:&錯誤];並檢查錯誤對象中是否有錯誤。 –

+0

收入是在viewDidLoad中初始化的NSMustableString。 –

回答

2

bneely的傳入非零錯誤指針的建議對於調試帶有錯誤參數的系統調用很有用。

它看起來像你從包中讀取CSV文件,修改數據,並試圖將數據保存回捆綁包中的文件。該軟件包在iOS中是隻讀的。 (注意,儘管模擬器並沒有強制執行此操作,但最後一次我檢查了你可以在SIM卡上寫入你的應用程序包,但是它在設備上無法正常工作

您需要編寫複製CSV文件的代碼如果它不存在,則從包中打包到文檔目錄中,然後進入打開CSV文件(在文檔中)修改數據並將其寫回的代碼。應用程序安裝後,將CSV文件從包中複製到文件中,然後文件上的所有操作發生在文件副本中,並且如果文件已存在,則不覆蓋該文件。

+0

你能告訴我,如果是最好的地方把它放在代表中的applicationDidFinishLaunchingWithOptions? –

+0

是的,檢查文件是否存在/從文檔中複製文件包中的文件將進入應用程序委託中的applicationDidFinishLaunchingWithOptions –

1

你打電話給writeToFile:atomically:encoding:error:與nil而不是一個有效的NSError參考。如果該方法返回錯誤,則不會知道該錯誤。

writeToFile:atomically:encoding:error:前行,插入此行

NSError *error = nil; 

現在改變到error:&error。然後添加此塊下方:

if (error) { 
    NSLog(@"writeToFile error: %@", [error localizedDescription]); 
} 

再次運行你的代碼,看看你是否得到一個錯誤寫入文件。

相關問題