0

私は、.csv ファイルから初期値を描画する単純なアプリを作成し、それらに追加してから、変更されたデータを同じ CSV ファイルに書き戻そうとしています。ターミナル ウィンドウの出力は正常に動作していることを示していますが、ファイルを確認したり、別のブートからアクセスしたりしても (Xcode の iPhone シミュレーターでテストしています)、動作していないようです。誰かが私が間違っているところを教えてもらえますか? これが私のコードです:

-(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");
}
4

2 に答える 2

2

nil 以外のエラー ポインターを渡すという bneely の提案は、エラー パラメーターを受け取るシステム コールのデバッグに役立ちます。

バンドルから CSV ファイルを読み取り、データを変更し、データをバンドル内のファイルに保存しようとしているように見えます。バンドルは iOS では読み取り専用です。(ただし、シミュレーターはこれを強制しないことに注意してください。前回チェックしたときは、SIM でアプリ バンドルに書き込むことができましたが、デバイスでは失敗しました。

CSV ファイルが存在しない場合は、起動時にバンドルからドキュメント ディレクトリに CSV ファイルをコピーするコードを記述する必要があります。次に、(ドキュメント内の) CSV ファイルを開くコードに陥り、データを変更して書き戻します。

そうすれば、インストール後にアプリを初めて起動するときに、CSV ファイルがバンドルからドキュメントにコピーされます。その後、ファイルに対するすべての操作はドキュメント内のコピーに対して行われ、ファイルが既に存在する場合は上書きしません。

于 2013-09-29T01:54:18.907 に答える