[英]How to remove extra data in my CSV Export?
這適用於我的使用Core Data
iOS
應用。
我要刪除的CSV導出中有一些其他信息。 以下是我的代碼和我所看到的示例。
碼:
NSOutputStream *stream = [[NSOutputStream alloc] initToMemory];
CHCSVWriter *writer = [[CHCSVWriter alloc] initWithOutputStream:stream encoding:NSUTF8StringEncoding delimiter:','];
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Notes"];
self.fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:nil];
for (NSString *instance in self.fetchedObjects) {
[writer writeLineOfFields:@[instance.description]];
}
[writer closeStream];
NSData *buffer = [stream propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
NSString *output = [[NSString alloc] initWithData:buffer encoding:NSUTF8StringEncoding];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths firstObject];
NSString * csvPath = [documentsDirectory stringByAppendingPathComponent:@"mydata.csv"];
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController * mailView = [[MFMailComposeViewController alloc] init];
mailView.mailComposeDelegate = self;
[[mailView navigationBar] setTintColor:[UIColor whiteColor]];
[mailView setSubject:@"My Subject Line"];
if (![[NSFileManager defaultManager] fileExistsAtPath:csvPath]) {
[[NSFileManager defaultManager] createFileAtPath:csvPath contents:nil attributes:nil];
}
BOOL res = [[output dataUsingEncoding:NSUTF8StringEncoding] writeToFile:csvPath 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 = %@", csvPath);
[mailView addAttachmentData:[NSData dataWithContentsOfFile:csvPath] mimeType:@"text/csv" fileName:@"mydata.csv"];
[self presentViewController:mailView animated:YES completion:nil];
}
} else {
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"Mail Error" message:@"Your device is not configured to send mail." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
輸出示例:
<Notes: 0x1c009fb30> (entity: Notes; id: 0xd000000000080000 <x-coredata://597EDC54-FFDE-43FA-8C61-DE67763C1D13/Notes/p2> ; data: {
// My data shows here.
})
我想要的是:
我只想從用戶輸入中保存數據。 不是有關實體,ID等的額外數據。
也:
我的數據最初不會加載到電子郵件中。 我必須進入我的UIViewController
,然后當我進入“設置”頁面並發送數據顯示的電子郵件時。 是什么原因造成的?
之所以得到該結果,是因為您依靠description
方法來獲取有關對象的信息。 該方法會產生一個字符串,而字符串正是您所看到的-不是CSV,或者很容易轉換為CSV。
我不熟悉CHCSVParser,但您似乎想重寫Notes
類中的description
以產生CHCSVParser可以處理的內容。 在這種情況下, NSManagedObject
的默認實現(這就是您現在正在使用的實現)不是一個好的選擇。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.