0

I have to say I'm beginner in Cocoa programing, and although I found 'recipe' how to do this in answer: Exporting the core data into csv via mail composer using CHCSVParser(by Dave DeLong) I still cannot make it work. I have too many holes in my knowledge. I'm trying to form CSV file from an Entity in data model, display that file for user and then sent that file via email. Nowever when I look at details in the answer linked above I'm stuck. Where I should put that part of code? In ViewControler that displays file for user? And then where exactly in viewDidLoad section, or somewhere else? Any tip, or even link for more detailed explanation of the whole procedure would be most welcome.

THANKS in advance!

The part of code with the errors Xcode throw (as a comment) is below:

#import "SendingViewController.h"
#import "DOAppDelegate.h"
#import "Record.h"
#import "CHCSVParser.h"

@interface SendingViewController (){

}
@end

@implementation SendingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSManagedObjectContext *moc = [self managedObjectContext]; 
//Error: No visible     @interface for 'sending View Controller' declares the selector 'managedObjectContext'
    NSEntityDescription *entityDescription = [NSEntityDescription
                                          entityForName:@"Record"   inManagedObjectContext:moc];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];


    request.predicate = [NSPredicate predicateWithFormat:@"rs_Record.name = %@", self.projectObject.name];
//Error: Property 'projectObject' not found on object of type 'sendingViewController *'


[request setEntity:entityDescription];
request.resultType = NSDictionaryResultType;

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Observer" ascending:YES];
[request setSortDescriptors:@[sortDescriptor]];

NSError *error;

NSArray *fetchedObjects = [moc executeFetchRequest:request error:&error];

//creating a csv CHCSVWriter
NSOutputStream *output = [NSOutputStream outputStreamToMemory];
CHCSVWriter *writer = [[CHCSVWriter alloc] initWithOutputStream:output encoding:NSUTF8StringEncoding delimiter:','];
//wrting header name for csv file
[writer writeField:@"Observer"];
[writer writeField:@"Time"];
[writer writeField:@"Lat"];
[writer writeField:@"Long"];
[writer writeField:@"Distance"];
[writer writeField:@"Doe"];
[writer writeField:@"Fawn"];
[writer writeField:@"Buck"];
[writer finishLine];



for (NSManagedObject *object in fetchedObjects)
{
    //getting the data from core data


    int doe_no = [[object valueForKey:@"doeNum"] intValue];
    int buck_no = [[object valueForKey:@"buckNum"] intValue];
    int fawn_no = [[object valueForKey:@"fawnNum"] intValue];
    int distance = [[object valueForKey:@"distance"] intValue];
    float latValue=[[object valueForKey:@"latitude"] floatValue];
    float lonValue=[[object valueForKey:@"longitude"] floatValue];

    NSString *obser=[object valueForKey:@"observer"];
    NSDate *time= [object valueForKey:@"time"];

    //writing that data to writer for csv file

    [writer writeField:[NSString stringWithFormat:@"%@",obser]];
    [writer writeField:[NSString stringWithFormat:@"%@",time]];
    [writer writeField:[NSString stringWithFormat:@"%f",latValue]];
    [writer writeField:[NSString stringWithFormat:@"%f",lonValue]];
    [writer writeField:[NSString stringWithFormat:@"%i",distance]];
    [writer writeField:[NSString stringWithFormat:@"%i",doe_no]];
    [writer writeField:[NSString stringWithFormat:@"%i",fawn_no]];
    [writer writeField:[NSString stringWithFormat:@"%i",buck_no]];


    [writer finishLine]; //finishing the writing of first row

}

[writer closeStream];


NSData *buffer = [output propertyForKey:NSStreamDataWrittenToMemoryStreamKey];

NSString *string = [[NSString alloc] initWithData:buffer encoding:NSUTF8StringEncoding];
//NSLog(@"string = %@",string);

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}




- (IBAction)showEmail:(id)sender {
Community
  • 1
  • 1
Lexa
  • 101
  • 1
  • 8
  • I added the code with the exact wording of errors I received. Is this sufficient or you need more info? – Lexa Oct 16 '14 at 23:20

2 Answers2

0

I don’t know what you are exactly asking for, but if you just don’t know when to make export to CSV, the easiest place for you is to export it just before sending email. So, user taps on “Send by email” button, which calls -(void)sendEmail:(id)sender;, then you make export to csv, prepare email, attach csv to it and present mail controller.

I also see, that you are looking for Record object by searching it by it’s name. You should just pass to predicate an object (or objectId) instead of it’s name property.

-- update about predicate

Your code:

request.predicate = [NSPredicate predicateWithFormat:@"rs_Record.name = %@", self.projectObject.name];

should be changed to this:

request.predicate = [NSPredicate predicateWithFormat:@"rs_Record = %@", self.projectObject];

thom_ek
  • 673
  • 4
  • 7
  • Thanks. This partially answered my plight. I am sorry for unclear question, the truth is, I do not know enough even to ask a decent question. To be honest I do not understand the second paragraph of your answer. I do not know what predicate actually means. Anyway, thank you for your answer. – Lexa Oct 21 '14 at 18:47
  • OK, I found something that might explain predicate. Is that correct? [link](https://www.iphonelife.com/blog/31369/unleash-your-inner-app-developer-tuning-core-data) – Lexa Oct 21 '14 at 20:23
0

The errors you mention in your code are pretty accurate.

The first one is telling you that you literally have not defined a property/method called "managedObjectContext" in your view controller. Everything you do in core data is going to need a context, so you need to supply that to your view controller some how. If you have an NSManagedObject, you can just use its .managedObjectContext property.

The second one is telling you that you literally have not defined a property called "projectObject" in your view controller.

I'm guessing that if you 1. create a public property on your view controller called "projectObject" and give it a value before presenting your view controller, 2. use projectObject.managedObjectContext instead of [self managedObjectContext], your errors will go away.

ghostatron
  • 2,620
  • 23
  • 27