简体   繁体   English

如何在CSV导出中删除多余的数据?

[英]How to remove extra data in my CSV Export?

This is for my iOS app using Core Data . 这适用于我的使用Core Data iOS应用。

I'm getting some extra information in my CSV export that I'd like to remove. 我要删除的CSV导出中有一些其他信息。 Below is my code and an example of what I am seeing. 以下是我的代码和我所看到的示例。

Code: 码:

    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];
    }

Output Example: 输出示例:

<Notes: 0x1c009fb30> (entity: Notes; id: 0xd000000000080000 <x-coredata://597EDC54-FFDE-43FA-8C61-DE67763C1D13/Notes/p2> ; data: {
    // My data shows here.
})

What I want: 我想要的是:

I just want the saved data from the user input. 我只想从用户输入中保存数据。 Not the extra data about the entity, id, etc. 不是有关实体,ID等的额外数据。

Also: 也:

My data does not load in the email initially. 我的数据最初不会加载到电子邮件中。 I have to go into my UIViewController and then when I go to my Settings page and send the email the data shows. 我必须进入我的UIViewController ,然后当我进入“设置”页面并发送数据显示的电子邮件时。 What would cause this? 是什么原因造成的?

You're getting that result because you're relying the description method to get information about your objects. 之所以得到该结果,是因为您依靠description方法来获取有关对象的信息。 That method produces a string, and the string is what you're seeing-- something that's not CSV, or easy to convert to CSV. 该方法会产生一个字符串,而字符串正是您所看到的-不是CSV,或者很容易转换为CSV。

I'm not familiar with CHCSVParser but it looks like you might want to override description in your Notes class to produce something that CHCSVParser can handle. 我不熟悉CHCSVParser,但您似乎想重写Notes类中的description以产生CHCSVParser可以处理的内容。 The default implementation on NSManagedObject -- which is what you're using now-- is not a good choice for this situation. 在这种情况下, NSManagedObject的默认实现(这就是您现在正在使用的实现)不是一个好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM