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 {