我一直在编写一个简单的应用程序,从.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");
}
答案 0 :(得分:2)
bneely建议传入一个非零错误指针对于调试带有错误参数的系统调用很有用。
在我看来,您正在从捆绑包中读取CSV文件,修改数据,并尝试将数据保存回捆绑包中的文件。该捆绑包在iOS中是只读的。 (请注意,模拟器不强制执行此操作。上次检查时,您可以在SIM卡上写入应用程序包,但在设备上失败。
如果不存在,您将需要编写将包中的CSV文件复制到文档目录中的代码。然后落入打开CSV文件的代码(在文档中)修改数据,并将其写回。
这样,首次在安装后启动应用程序时,CSV文件将从包中复制到文档中。之后,文件上的所有操作都在文档中的副本上进行,如果文件已经存在,则不会覆盖该文件。
答案 1 :(得分:1)
您使用nil调用writeToFile:atomically:encoding:error:
而不是有效的NSError引用。如果该方法返回错误,您将无法了解它。
在writeToFile:atomically:encoding:error:
之前的行上,插入此行
NSError *error = nil;
现在将error:nil
更改为error:&error
。然后在它下面添加这个块:
if (error) {
NSLog(@"writeToFile error: %@", [error localizedDescription]);
}
再次运行代码,看看是否在编写文件时出错。