简体   繁体   中英

How to remove extra data in my CSV Export?

This is for my iOS app using Core Data .

I'm getting some extra information in my CSV export that I'd like to remove. 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.

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. What would cause this?

You're getting that result because you're relying the description method to get information about your objects. That method produces a string, and the string is what you're seeing-- something that's not CSV, or easy to convert to 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. The default implementation on NSManagedObject -- which is what you're using now-- is not a good choice for this situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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