我有一个iOS应用程序从表单中获取数据,然后通过电子邮件将其作为csv发送。目前它将csv保存为静态文件名,但我希望它开始将其保存为NSString* fileName = @"file.csv";
。如果它更容易重新定义一个新变量的日期那么就是它,尝试过它但也无法使它工作。
目前它看起来像这样:
NSString* fileName = @"%@%@ .csv",FromEmail,dateString;
我试过这个,其他一些小的语法更改没有运气:
NSString *FromEmail = [userDefault objectForKey:@"FromEmail"];
FromEmail在HomeView.m中定义如下:
//CSV function
NSMutableString *csv = [NSMutableString stringWithString:@""];
//add your content to the csv
[csv appendFormat:@"From,Date,Client,Time,Notes,Hardware,AfterHours,NoCharge\n%@,%@,%@,%@,%@,%@,%@,%@",FromEmail,dateString,txtClient.text,txtTime.text,txtNotes.text,txtHardware.text,isAfterHours,isDiscount];
NSString* filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//NSString* fileName = @"file.csv";
NSString* fileName = @"%@%@ .csv",FromEmail,dateString;
NSString* fileAtPath = [filePath stringByAppendingPathComponent:fileName];
if (![[NSFileManager defaultManager] fileExistsAtPath:fileAtPath]) {
[[NSFileManager defaultManager] createFileAtPath:fileAtPath contents:nil attributes:nil];
}
BOOL res = [[csv dataUsingEncoding:NSUTF8StringEncoding] writeToFile:fileAtPath atomically:NO];
if (!res) {
[[[UIAlertView alloc] initWithTitle:@"Error Creating CSV" message:@"Check your permissions to make sure this app can create files so you may email the app data" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles: nil] show];
}else{
NSLog(@"Data saved! File path = %@", fileName);
[picker addAttachmentData:[NSData dataWithContentsOfFile:fileAtPath]
mimeType:@"text/csv"
fileName:@"file.csv"];
[self presentViewController:picker animated:YES completion:nil];
}
//END CSV Function
这是整个csv制作部分:
{{1}}
我怀疑它的东西非常简单,但它让我难以忍受多年
答案 0 :(得分:0)
Hot Licks是对的,我的日期格式错了。它抛出了一个错误,因为日期部分是使用正斜杠格式化的,这正在改变它保存的路径。
我使用以下代码为CSV文件名添加了一个单独的日期格式化程序:
NSDate *csvDate = [NSDate date];
NSDateFormatter *csvFormatter = [[NSDateFormatter alloc] init];
[csvFormatter setDateFormat:@"ddMMyy-Hm"];
NSString *csvDateString = [csvFormatter stringFromDate:csvDate ];
并将使文件名变量的行更改为:
NSString* fileName = [NSString stringWithFormat:@"%@%@.csv",FromEmail,csvDateString];
感谢您的帮助